JavaScript Guess Number Game - Loop Implementation

 
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
Generate random
Guess
R = G
Got it
R > G
Low
High
End
 
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
 
<!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>
</body>
</html>
 
Note that I write
theRanDum on the
screen for testing
purposes.
start
Generate random
Guess
R = G
Got it
R > G
Low
High
End
 
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.
 
Insert a decision here
To continue if the guess was not correct.
Loop back to just before the guess.
start
Generate random
Guess
R = G
Got it
R > G
Low
High
End
R != G
 
If the random number
and the guess are
not equal I go back
and guess again.
Note that != means
not equal to.
 
The do loop I am
using will have the
do right before the
guess so that is
the entry point
when I loop back.
 
<!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>
</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.
start
Generate random
Guess
R = G
Got it
R > G
Low
High
End
R != G
 
Now I need to give
the user the
opportunity to play
again.  I will put the
question after I get a
match.
 
The entry point will
come before I generate
the random number so
that each new game will
have a new random
number.
 
<!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>
</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.
start
Generate random
Guess
R = G
Got it
R > G
Low
High
End
R != G
 
Prompt the user
about playing again.
 
The play again do
loop comes in
before the random
number is
generated.
Play again
Again != N
 
<!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>");
       } while (playAgain != "N");
</script>
</body>
</html>
Slide Note
Embed
Share

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.

  • JavaScript
  • Guessing Game
  • Loop Implementation

Uploaded on Sep 21, 2024 | 0 Views


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


  1. 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

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

  3. <!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.

  4. 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

  5. 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

  6. <!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.

  7. 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

  8. <!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.

  9. 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

  10. <!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>");

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#