GUESS THE NUMBER GAME
Chapter2: Using Data
Section: Chapter Questions
Problem 2GZ
Related questions
Question
Using JavaScript + html
![GUESS THE NUMBER GAME
In this task, you will create a program that plays a guess the number game with the user. Your
program will generate a random secret number by setting a variable equal to
Math.floor(Math.random()*20) + 1
(This expression generates a random number from 1 to 20 and is included in the starter code).
Then, it will allow the user up to five guesses to guess the correct number.
After generating the secret number. Your program should prompt the user: "Guess a number
from 1-20 (secret number)!". The number in the parenthesis should be the actual secret number
(e.g., "Guess a number from 1-20 (14)!").
The program should handle the following cases and allow up to five guesses.
1. If the user enters a number less than 1 or greater than 20, it should generate an alert
instructing them to guess between 1 and 20. Then, if they still have guesses left, it
should prompt them for another guess.
2. If the guess matches the secret number, it should end the game and print, "You win!" in
the <h2></h2> tags with id="outcome".
3. If the guess is smaller than the secret number, it should generate an alert that says, "Too
small!". Then, if they still have guesses left, it should prompt them for another guess.
4. If the guess is larger than the secret number, it should generate an alert that says, "Too
large!". Then, if they still have guesses left, it should prompt them for another guess.
5.
f they have made five failed guesses, it should end the game and print, "You lose!" in
the <h2></h2> tags with id="outcome". Any guess which is not the correct number
counts as a failed guess regardless of whether the guess was in the range 1-20 or not.
In all cases, it should print "The number was " followed by the secret number in the <p></p>
tags with id="answer".
ADDITIONAL CLARIFICATIONS
You can assume the user enters a number in a reasonable form (e.g., using numeric
characters like "6", "-2", or "23"). You do NOT have to handle any kind of word input like
random letters or written numbers (e.g., "six") for this program.
You must use a loop.
●](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fdf5b15e2-e6d6-41f9-adf4-8004644211b0%2F77a53358-64ab-4b81-91aa-b92a24e27fd3%2F6q4iay_processed.jpeg&w=3840&q=75)
Transcribed Image Text:GUESS THE NUMBER GAME
In this task, you will create a program that plays a guess the number game with the user. Your
program will generate a random secret number by setting a variable equal to
Math.floor(Math.random()*20) + 1
(This expression generates a random number from 1 to 20 and is included in the starter code).
Then, it will allow the user up to five guesses to guess the correct number.
After generating the secret number. Your program should prompt the user: "Guess a number
from 1-20 (secret number)!". The number in the parenthesis should be the actual secret number
(e.g., "Guess a number from 1-20 (14)!").
The program should handle the following cases and allow up to five guesses.
1. If the user enters a number less than 1 or greater than 20, it should generate an alert
instructing them to guess between 1 and 20. Then, if they still have guesses left, it
should prompt them for another guess.
2. If the guess matches the secret number, it should end the game and print, "You win!" in
the <h2></h2> tags with id="outcome".
3. If the guess is smaller than the secret number, it should generate an alert that says, "Too
small!". Then, if they still have guesses left, it should prompt them for another guess.
4. If the guess is larger than the secret number, it should generate an alert that says, "Too
large!". Then, if they still have guesses left, it should prompt them for another guess.
5.
f they have made five failed guesses, it should end the game and print, "You lose!" in
the <h2></h2> tags with id="outcome". Any guess which is not the correct number
counts as a failed guess regardless of whether the guess was in the range 1-20 or not.
In all cases, it should print "The number was " followed by the secret number in the <p></p>
tags with id="answer".
ADDITIONAL CLARIFICATIONS
You can assume the user enters a number in a reasonable form (e.g., using numeric
characters like "6", "-2", or "23"). You do NOT have to handle any kind of word input like
random letters or written numbers (e.g., "six") for this program.
You must use a loop.
●
Expert Solution
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 4 steps with 3 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781305480537/9781305480537_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781305480537/9781305480537_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
![C++ for Engineers and Scientists](https://www.bartleby.com/isbn_cover_images/9781133187844/9781133187844_smallCoverImage.gif)
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr