javascript - Program works corectly two times after that it goes wrong -


after entering correct answer twice, subsequent try, if correct answer entered again, shows "try again" output. causing strange behavior?

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">      <head>         <meta http-equiv="content-type" content="text/html; charset=utf-8" />         <title>untitled document</title>         <script type="text/javascript">             var answer;             var firstnum;             var secondnum;             var check = true;             question();              function question() {                 firstnum = math.floor(math.random() * 101);                 secondnum = math.floor(math.random() * 101);                 answer = firstnum * secondnum;                 document.writeln("<h1>how " + firstnum + " times " + secondnum + "</h1>");                 document.writeln(answer);                 document.write('<input name="inputanswer" type="text" id="inputans"/>');                 document.write('<input name="submitbtn" type="submit" value="submit answer" onclick="validate()"/>');             }              function validate() {                 if (document.getelementbyid("inputans").value == answer) {                     document.write("good");                     check = true;                     result();                 } else {                     check = false;                     document.write("try again");                     result();                 }             }              function result() {                 if (check == true)                     question();                 else {                     document.writeln("<h1>how " + firstnum + " times " + secondnum + "</h1>");                     document.writeln(answer);                     document.write('<input name="inputanswer" type="text" id="inputans"/>');                     document.write('<input name="submitbtn" type="submit" value="submit answer" onclick="validate()"/>');                 }             }         </script>     </head>      <body></body>  </html> 

i have similar type of solution in different way. try it.

    <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"                                          "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>untitled document</title>  <h1>how <span id="firstnumber"></span>  times <span id="secondnumber"></span></h1>               answer                 <input name="inputanswer" type="text" id="inputans"/>                 <input name="submitbtn" type="submit" value="submit answer" onclick="validate()"/>                 <span id="answer"></span> <script type = "text/javascript">     var answer;     var firstnum;     var secondnum;     var check= true;    var count=0; question();       function question()     {                 firstnum= math.floor(math.random()*101);             secondnum= math.floor(math.random()*101);              answer = firstnum * secondnum;                  document.getelementbyid("firstnumber").innerhtml=firstnum;                document.getelementbyid("secondnumber").innerhtml=secondnum;      }        function validate()     {         if(document.getelementbyid("inputans").value == answer)         {               check= true;           }         else         {             check= false;           }         result();       }      function result()     {         if(check== true)         {             document.getelementbyid("answer").innerhtml="correct answer"             count=count+1;           }         else         {             document.getelementbyid("answer").innerhtml="wrong answer.<br>you've corrected total "+count+" questions.";         }         document.getelementbyid("inputans").value="";          question();       }      </script> 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -