In this project, you will create a simple "guess the number" game.
The program will generate a random number between 1 and 100, and the user will have to guess what the number is.
After each guess, the program will tell the user whether the number they guessed is higher or lower than the actual number.
The game will continue until the user correctly guesses the number.
-
Write a function called
generateRandomNumberthat generates a random number between 1 and 100.
You can use theMath.random()function to generate a random decimal between 0 and 1, and then multiply it by 100 and round it down to get a random integer between 1 and 100. -
Write a function called
getUserGuessthat prompts the user to enter a number between 1 and 100.
Use theprompt()function to get the user's input, and then convert it to a number using theparseInt()function. -
Write a function called
compareNumbersthat takes two arguments: the user's guess and the actual number.- If the user's guess is higher than the actual number, display "Too high!" to the user & return
false. - If the user's guess is lower than the actual number, display "Too low!" to the user & return
false. - If the user's guess is equal to the actual number, display "Correct!" to the user & return
true.
- If the user's guess is higher than the actual number, display "Too high!" to the user & return
-
Write a function called
playGamethat brings everything together.
Guess the Number!
I'm thinking of a number between 1 and 100.
Enter your guess: 50
Too high!
Enter your guess: 25
Too low!
Enter your guess: 37
Too high!
Enter your guess: 31
Too high!
Enter your guess: 28
Too low!
Enter your guess: 29
Correct! Congratulations!
-
Don't forget to use the
parseInt()function to convert the user's input to a number in thegetUserGuessfunction. -
Use
alret()to display messages to the user. -
Break the problem down into small, manageable parts, and write one function at a time.
-
Test your functions as you write them to make sure they work correctly.
-
(BONUS) Use a
whileloop to keep prompting the user for a guess until they correctly guess the number.