+ 3

Why the 'x' variable not getting the Fresh Value

<script> function start(){ var fst = document.getElementById("fst").innerHTML = Math.floor(Math.random()*10); var snd = document.getElementById("snd").innerHTML = Math.floor(Math.random()*10); return fst+snd; } var x = start() function validate(){ var txt = document.getElementById("txt").value; if(txt==x){ console.log('Correct Answer'); } else console.log('Wrong Answer'); start(); } </script> https://code.sololearn.com/WTP9p3g5XnIZ/?ref=app

12th Jun 2019, 3:56 AM
zexu knub
zexu knub - avatar
3 Answers
+ 4
Line 53: x = start();
12th Jun 2019, 5:07 AM
Anna
Anna - avatar
+ 3
Your `start` function could be clearer this way: function start(){ var fst = Math.floor(Math.random()*10); document.getElementById("fst").innerHTML = fst; var snd = Math.floor(Math.random()*10); document.getElementById("snd").innerHTML = snd; return fst + snd; }
12th Jun 2019, 5:27 AM
Ipang
+ 1
Anna thankyou ma'am its working & please explain why we have to assign x to this calling function when all this start doing is just calling the function again
12th Jun 2019, 5:17 AM
zexu knub
zexu knub - avatar