/    Sign up×
Community /Pin to ProfileBookmark

Repeating a function

Hi, I need help… I have unit converter for students and I need to have more than one task… Well, at least 5 or 10, that would be great. It would be helpfull if you could modify my code. Here it is:

[code]
<!DOCTYPE HTML>
<html >
<head>

<title>Unit converter</title>

</head>
<body>

<div id=”container”>
<h1>Převodník jednotek</h1>

<form action=”#”>
<p>Převeď centimetry na metry. <br> Jako destinnou čárkou používej tečku.</p>
<input id=”generated” type=”text” readonly>
<label> cm = </label>
<input id=”number” >
<label> m</label>
<label id=”test”></label><br>
<br>
<button id=”button” type=”button”>Zkontrolovat</button>
</form>
</div>

<script>
(function( d ) {

d.getElementsByTagName( ‘form’)[0].reset();
d.getElementById( ‘generated’ ).value = Math.floor( Math.random() * 1001 );
d.getElementById( ‘button’ ).addEventListener( ‘click’,
function() {
var x = d.getElementById( ‘generated’ ).value;
var y = d.getElementById( ‘number’ ).value;
var z = 100;
var a = y * z;
if ( a == x ) {
text = “✔”;
}
else {
text = “✘”;
}
document.getElementById(“test”).innerHTML = text;
}, false );
}( document ));
</script>

</body>
</html>
[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@volverNov 17.2019 — When you want to repeat a task you probably want to use a loop or make a reusable function here's what I think you want

&lt;!DOCTYPE HTML&gt;<br/>
&lt;html &gt;<br/>
&lt;head&gt;<br/>
&lt;title&gt;Unit converter&lt;/title&gt;<br/>
&lt;/head&gt;<br/>
&lt;body&gt;<br/>
&lt;div id="container"&gt; <br/>
&lt;h1&gt;Převodník jednotek&lt;/h1&gt;<br/>
&lt;form action="#"&gt;<br/>
&lt;p&gt;Převeď centimetry na metry. &lt;br&gt; Jako destinnou čárkou používej tečku.&lt;/p&gt;<br/>
&lt;input id="generated-0" type="text" readonly&gt;<br/>
&lt;label&gt; cm = &lt;/label&gt;<br/>
&lt;input id="number-0" &gt;<br/>
&lt;label&gt; m&lt;/label&gt;<br/>
&lt;label id="test-0"&gt;&lt;/label&gt;<br/>
&lt;button class="button" type="button" onclick="checkAnswer(this)" value=0&gt;Zkontrolovat&lt;/button&gt;<br/>
&lt;br&gt;&lt;br&gt;<br/>
&lt;input id="generated-1" type="text" readonly&gt;<br/>
&lt;label&gt; cm = &lt;/label&gt;<br/>
&lt;input id="number-1" &gt;<br/>
&lt;label&gt; m&lt;/label&gt;<br/>
&lt;label id="test-1"&gt;&lt;/label&gt;<br/>
&lt;button class="button" type="button" onclick="checkAnswer(this)" value=1&gt;Zkontrolovat&lt;/button&gt;<br/>
&lt;br&gt;&lt;br&gt;<br/>
&lt;input id="generated-2" type="text" readonly&gt;<br/>
&lt;label&gt; cm = &lt;/label&gt;<br/>
&lt;input id="number-2" &gt;<br/>
&lt;label&gt; m&lt;/label&gt;<br/>
&lt;label id="test-2"&gt;&lt;/label&gt;<br/>
&lt;button class="button" type="button" onclick="checkAnswer(this)" value=2&gt;Zkontrolovat&lt;/button&gt;<br/>
&lt;br&gt;&lt;br&gt;<br/>
&lt;input id="generated-3" type="text" readonly&gt;<br/>
&lt;label&gt; cm = &lt;/label&gt;<br/>
&lt;input id="number-3" &gt;<br/>
&lt;label&gt; m&lt;/label&gt;<br/>
&lt;label id="test-3"&gt;&lt;/label&gt;<br/>
&lt;button class="button" type="button" onclick="checkAnswer(this)" value=3&gt;Zkontrolovat&lt;/button&gt;<br/>
&lt;br&gt;&lt;br&gt;<br/>
&lt;input id="generated-4" type="text" readonly&gt;<br/>
&lt;label&gt; cm = &lt;/label&gt;<br/>
&lt;input id="number-4" &gt;<br/>
&lt;label&gt; m&lt;/label&gt;<br/>
&lt;label id="test-4"&gt;&lt;/label&gt;<br/>
&lt;button class="button" type="button" onclick="checkAnswer(this)" value=4&gt;Zkontrolovat&lt;/button&gt;<br/>
&lt;br&gt;&lt;br&gt;<br/>
&lt;/form&gt;<br/>
&lt;/div&gt;<br/>
&lt;script&gt;<br/>
function checkAnswer(i) {<br/>
i = i.value;<br/>
var x = document.getElementById(
generated-${i} ).value;<br/>
var y = document.getElementById(
number-${i} ).value;<br/>
var z = 100;<br/>
var a = y * z;<br/>
if ( a == x ) {<br/>
text = "✔";<br/>
}<br/>
else {<br/>
text = "✘";<br/>
}<br/>
document.getElementById(
test-${i}).innerHTML = text; <br/>
}<br/>
(function( d ) {<br/>
d.getElementsByTagName( 'form')[0].reset();<br/>
for(let i = 0;i&lt;5;i++) {<br/>
console.log(i);<br/>
d.getElementById(
generated-${i} ).value = Math.floor( Math.random() * 1001 );<br/>
}<br/>
}( document ));<br/>
&lt;/script&gt;<br/>
&lt;/body&gt;<br/>
&lt;/html&gt;
Copy linkTweet thisAlerts:
@rici1990authorNov 19.2019 — Yeah, that's what I meant... But now it doesn't generate the numbers, but thanks for that ;)
Copy linkTweet thisAlerts:
@rici1990authorNov 19.2019 — Well, technically it is what I meant, but I want only one button to check all of the tasks up... And now it doesn't generate numbers, so I maybe there's a little mistake. Still thanks for that :D
Copy linkTweet thisAlerts:
@volverNov 19.2019 — @rici1990#1610986

Oh seems like the website here removes any ` from the code that's what ruined it , anyway here try this

https://codepen.io/volver805/pen/JjjxZeo
×

Success!

Help @rici1990 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...