Presentation is loading. Please wait.

Presentation is loading. Please wait.

The three programs to look at are at:

Similar presentations


Presentation on theme: "The three programs to look at are at:"— Presentation transcript:

1 The three programs to look at are at:
Guess Games The three programs to look at are at:

2 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. Works best in Firefox Generate random Guess R = G R > G Got it High Low End

3 Note that I write theRanDum on the screen for testing purposes.
<!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"); document.write("Your guess is to high"); </script> </body> </html> Note that I write theRanDum on the screen for testing purposes.

4

5 Now I want to set up a loop to continue until the answer is correct
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 R > G Got it High Low Insert a decision here To continue if the guess was not correct. Loop back to just before the guess. End

6 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 R > G Got it 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

7

8 I choose a do loop since I always want to do the loop at least once.
<!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"); document.write("Your guess is to high"); } while (theRanNum != myGuess); </script> </body> </html> 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.

9 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 R > G Got it 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

10 <!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"); document.write("Your guess is to high"); } while (theRanNum != myGuess); </script> </body> </html> The do will go in here The prompt to ask the user about playing again and the question about the response will go here.

11 start The play again do loop comes in before the random number is generated. Generate random Guess R = G R > G Got it High Low Prompt the user about playing again. R != G Play again Again != N End

12 <!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); 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"); document.write("Your guess is to high"); } while (theRanNum != myGuess); playAgain = window.prompt("Do you want to play again, Y or N"); } while (playAgain != "N"); </script> </body> </html>

13

14


Download ppt "The three programs to look at are at:"

Similar presentations


Ads by Google