Jump to content

Advice needed.


Euphorius

Recommended Posts

Hello, im beginner in to programming and i need an advice. I'm almost sure the problem is in the nested (second) if and i dont know how to solve it. The goal of it is when the user recognize the number to be asked with confirm, if he need another number for recognizing or quit the page or window. The way i wrote the code it aways show the confirm box when i press the submit button.Thanks.

jsStuding.html

Link to comment
Share on other sites

<html> <head> <title> JS studing</title> </head> <body onload="start()"> <b><p id="show">Please enter a number between 1-100</p></b> <input id="test" type="number"> <button type="button" onclick="test1()">Submit</button> <p id="result"></p> <script> function start() { var n=Math.floor((Math.random())*100+1); var d=document.getElementById('test').value; } function test1() { if (n==d) { alert("Well Done! You Have Guessed The Number!") } else { alert("You Have Guessed The Wrong Number. "+d+" Is Not Correct.") } } </script> </body></html> Hopefully this helps.

  • Like 1
Link to comment
Share on other sites

Thanks for the answer.But the way i want it to be is a bit more complicated, something like that ->a=Math.floor((Math.random())*100+1);function test1(){document.getElementById("Random number").innerHTML=a;var x=document.getElementById("test").value;if (a==x) { confirm("Gratz, you've entered the right number"); } else if (a<x) { document.getElementById("result").innerHTML="Try lower number"; }else { document.getElementById("result").innerHTML="Try higher number"; }}my goal is when 'a==x' and the confirm box appear if you press ok, the program will generate another number (if possible, if not... refresh the page ), if you press cancel, the program will exit the page and i dont know how to do it. In the attached file it works but when you enter a number even if 'a' is not equal to 'x' the program pop up this confirm box and do nothing after that.Also i've put the random method as global, because if it is inside the function every time you press Submit, it will generate new number. Correct me if im wrong. :)

Edited by Euphorius
Link to comment
Share on other sites

Also i've put the random method as global, because if it is inside the function every time you press Submit, it will generate new number. Correct me if im wrong. :)
How about, when you get the correct answer it will re-generate the answer if (a==x) { confirm("Gratz, you've entered the right number"); a=Math.floor((Math.random())*100+1);} [EDIT]Would that work? You are not wrong, but that is what I would do :)[/EDIT] Edited by LucasMars123
  • Like 1
Link to comment
Share on other sites

Confirm returns true if they clicked the OK button, or else it returns false. You can use that to decide what you want to do. var again = confirm('try again?');if (again){...}
you meanvar conf=confirm("Gratz you've entered the right number, would you like another try ?");var x=document.getElementById("test").value;if (a==x){if (conf==true){location.reload(true);}else{window.close();} Just to remove ==true ?i've tryed but even if (a!=x) the confirm box appears... both ways Edited by Euphorius
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...