JavaScript Guess Number Game - Loop Implementation
In this JavaScript guessing game, a random number is generated, and the user is prompted to guess the number. If the guess is incorrect, the user receives feedback on whether the guess was too high or too low. A loop using a do...while statement is implemented to allow the user to keep guessing until the correct number is guessed. The game provides a simple and interactive way to test and improve guessing skills.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
Guess Games The three programs to look at are at: http://cisweb.bristolcc.edu/~pgrocer/CIS122S13/loopsetcJS/guessgameone5.html http://cisweb.bristolcc.edu/~pgrocer/CIS122S13/loopsetcJS/guessgameonegame5.html http://cisweb.bristolcc.edu/~pgrocer/CIS122S13/loopsetcJS/guessgamemulti5.html
start I generate a random number and prompt the user for a guess. Then I compare and process. Note this version allows for only one guess. Generate random Guess Works best in Firefox R = G Got it R > G High Low End
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JavaScript guess number game</title> </head> <body> <h1>GUESSING GAME</h1> <script type="text/javascript"> var theRanNum = Math.floor(Math.random()*5)+1; document.write(theRanNum); document.write("<br>"); var myGuess = parseInt(window.prompt("Enter your quess")); if (theRanNum == myGuess) { document.write("You got it"); } else { if (theRanNum > myGuess) { document.write("Your guess is to low"); } else { document.write("Your guess is to high"); } } </script> Note that I write theRanDum on the screen for testing purposes.
Now I want to set up a loop to continue until the answer is correct. I have decided to use a do...while so the question will be at the bottom. start Generate random Guess R = G Got it R > G High Low Insert a decision here To continue if the guess was not correct. Loop back to just before the guess. End
start The do loop I am using will have the do right before the guess so that is the entry point when I loop back. Generate random Guess R = G Got it R > G High Low If the random number and the guess are not equal I go back and guess again. Note that != means not equal to. R != G End
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JavaScript guess number game</title> </head> <body> <h1>GUESS GAME</h1> <script type="text/javascript"> var theRanNum = Math.floor(Math.random()*5)+1; document.write(theRanNum); do { document.write("<br>"); var myGuess = parseInt(window.prompt("Enter your quess")); if (theRanNum == myGuess) { document.write("You got it"); } else { if (theRanNum > myGuess) { document.write("Your guess is to low"); } else { document.write("Your guess is to high"); } } } while (theRanNum != myGuess); </script> Note that the random number is generate once outside the do loop while the user guess is prompted when you look back for another try. I choose a do loop since I always want to do the loop at least once.
The entry point will come before I generate the random number so that each new game will have a new random number. start Generate random Guess R = G Got it R > G High Low Now I need to give the user the opportunity to play again. I will put the question after I get a match. R != G End
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JavaScript guess number game</title> </head> <body> <h1>GUESS GAME</h1> <script type="text/javascript"> var theRanNum = Math.floor(Math.random()*5)+1; document.write(theRanNum); do { document.write("<br>"); var myGuess = parseInt(window.prompt("Enter your quess")); if (theRanNum == myGuess) { document.write("You got it"); } else { if (theRanNum > myGuess) { document.write("Your guess is to low"); } else { document.write("Your guess is to high"); } } } while (theRanNum != myGuess); </script> The do will go in here The prompt to ask the user about playing again and the question about the response will go here.
start The play again do loop comes in before the random number is generated. Generate random Guess R = G Got it R > G High Low Prompt the user about playing again. R != G Play again Again != N End
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JavaScript guess number game</title> </head> <body> <script type="text/javascript"> var playAgain = "Y"; do { var theRanNum = Math.floor(Math.random()*5)+1; document.write(theRanNum); do { document.write("<br>"); var myGuess = parseInt(window.prompt("Enter your quess")); if (theRanNum == myGuess) { document.write("You got it"); } else { if (theRanNum > myGuess) { document.write("Your guess is to low"); } else { document.write("Your guess is to high"); } } } while (theRanNum != myGuess); playAgain = window.prompt("Do you want to play again, Y or N"); document.write("<br>");