javascript - How do I ask a user if they want to play the game again? -
i want ask user if want play game again @ end if guess correctly. if no want end program. yes-> play again , no->end program
<html> <head> <title>numberguesser</title> <script> var guess = 99; var target = 0; var turns = 0; var msg = ""; target = math.floor(math.random() * 100) + 1; alert (target); msg = "guess number between 1 , 100 !. \n"; msg += "guess number, , i'll tell if it's high, \n"; msg += "too low, or correct. "; alert (msg); guess = eval(prompt("what guess?", "")); while ( guess != target){ if (guess > target){ guess = prompt (turns + ". high!! try again!!", ""); } // end if if (guess < target){ guess = prompt (turns + ". low!! try again!!", ""); } // end if if (guess == target){ msg = "you win!!! \n"; alert (msg); } // end if } // end while </script> </head> <body> <center><center> <h1>numberguesser<br></h1> </center> <hr> </body> </html> thanks guys, i'm having rough time in javascript.
you want whole program again until stop doing it, need put while loop around whole program (with user-prompt @ end).
var guess = 99; var target = 0; var turns = 0; var msg = ""; var keep_looping = true; // new loop. // whole program inside loop, whole program // repeated on , on until "keep_looping" false while (keep_looping) { target = math.floor(math.random() * 100) + 1; alert (target); msg = "guess number between 1 , 100 !. \n"; msg += "guess number, , i'll tell if it's high, \n"; msg += "too low, or correct. "; alert (msg); guess = eval(prompt("what guess?", "")); while ( guess != target){ if (guess > target){ guess = prompt (turns + ". high!! try again!!", ""); } // end if if (guess < target){ guess = prompt (turns + ". low!! try again!!", ""); } // end if if (guess == target){ msg = "you win!!! \n"; alert (msg); } // end if } // end while // we're done program time - need deicde if w want loop again... // prompt user if want again // if reply "no" // change "keep_looping" false }
Comments
Post a Comment