html - My javascript function in the code given below is getting called twice -
the below code simple number guessing game. function guess()
getting called twice. @ loss of logic why it's happening.
<!doctypte html> <html> <head><title>number guessing game version 1.0</title></head> <body> <form onsubmit="guess();return false;"> <p><h2>i host, human. thinking of number between 0 , 100, including both</h2></p> <p><input type="text" id="inputid" autocomplete="off"></input><button id="submitbutton" onclick="guess()">guess!!</button></p> <p><span id="msgid"></span></p> <p>guesses remaining:<span id="guessid"></span></p> </body> </form> <script language="javascript"> var doublerandom = math.random(); var guessesleft = parseint("10"); var intrandom = math.round((doublerandom*100)); var spanobj = document.getelementbyid("msgid"); var guessobj = document.getelementbyid("guessid"); guessobj.innerhtml=guessesleft; function guess() { var guessednumber = document.getelementbyid("inputid").value; alert(23); if(guessednumber==null || guessednumber.trim()==''){ spanobj.innerhtml="type something, human"; return; } if(isnan(guessednumber)){ spanobj.innerhtml="that better number, human."; return; }else{ if(guessednumber>100){ spanobj.innerhtml="that better number between 0 , 100, human."; return; }else{ spanobj.innerhtml=""; } } var accurateanswer = math.round(guessednumber); var difference = guessednumber-intrandom; if(difference>45){ spanobj.innerhtml="that's way high, human"; return; }else if(difference<-45){ spanobj.innerhtml="that's way low, human"; }else if(difference<=45 && difference>0){ spanobj.innerhtml="that's high, human"; }else if(difference>=-45 && difference<0 ){ spanobj.innerhtml="that's low, human"; }else{ spanobj.innerhtml="bingo!! got it!! refresh play agin."; } if(guessesleft<=0){ spanobj.innerhtml="you have exhausted number of guesses. try again. refreshing game...."; settimeout("location.reload(true)", 3000); } guessesleft=guessesleft-1; guessobj.innerhtml=guessesleft; } </script> </html>
that's because calling twice: once in button's onclick
event, , once in form's onsubmit
event. delete 1 of them.
Comments
Post a Comment