Euphorius Posted April 28, 2013 Share Posted April 28, 2013 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 More sharing options...
LucasMars123 Posted April 28, 2013 Share Posted April 28, 2013 I think I may have a solution... I will post the result in a minute. Link to comment Share on other sites More sharing options...
LucasMars123 Posted April 28, 2013 Share Posted April 28, 2013 (edited) Instead of an editbox, would it work if it used a slider? [EDIT] Nevermind [/EDIT] [EDIT 2]It might take longer[/EDIT 2] Edited April 28, 2013 by LucasMars123 Link to comment Share on other sites More sharing options...
LucasMars123 Posted April 28, 2013 Share Posted April 28, 2013 <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. 1 Link to comment Share on other sites More sharing options...
Euphorius Posted April 28, 2013 Author Share Posted April 28, 2013 (edited) 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 April 28, 2013 by Euphorius Link to comment Share on other sites More sharing options...
LucasMars123 Posted April 28, 2013 Share Posted April 28, 2013 (edited) 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 April 28, 2013 by LucasMars123 1 Link to comment Share on other sites More sharing options...
Euphorius Posted April 28, 2013 Author Share Posted April 28, 2013 This is good idea. Thanks.. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 29, 2013 Share Posted April 29, 2013 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){...} Link to comment Share on other sites More sharing options...
Euphorius Posted April 30, 2013 Author Share Posted April 30, 2013 (edited) 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 April 30, 2013 by Euphorius Link to comment Share on other sites More sharing options...
justsomeguy Posted April 30, 2013 Share Posted April 30, 2013 Well yeah, you have the confirm box before the if statement where you compare a and x, not inside it. If a and x are true then you want to show the confirm box, the code should read the same way. Link to comment Share on other sites More sharing options...
Euphorius Posted May 1, 2013 Author Share Posted May 1, 2013 I've made it ! Exactly as i wanted to be. Thank you very much, some guy! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now