Asked by Anonymous
Most of the program for a simple number guessing game has been written. You aren’t expected to understand all of it right now, but if you read from top to bottom, we’ve broken down the program into the following smaller steps:
Set the limits of the number range in min and max variables
Print messages to introduce the game
Randomly select a number within the appropriate range
Ask the user to guess a number (YOUR TASK)
Check to see if the user has guessed the correct number
Print out the correct answer
Your Task:
The only code step missing in the game is to ask the user for their number guess. You should complete this on line 15 of the starter code. You should ask them for an integer and store the value in a variable named guess.
If done successfully, the game should run without error and ask the user to guess a number in the range. Here is a sample output:
This program plays a guessing game.
The computer is thinking of a value between 1 and 10.
What is your guess? 5
Your guess of 5 was too high.
The number was 4.
Plain text
Status: Not Submitted
2.4.3 Exploration: Guessing Game
default.js
function main() {
// Defines the limits of the guessing game
let min = 1;
let max = 5;
// Introduces the game
console.log("This program plays a guessing game.");
console.log("The computer is thinking of a value between " + min + " and " + max + ".");
// Randomly selects a number between min and max
let numberToGuess = Randomizer.nextInt(1, 5);
// ------> Enter your code on the next line that will ask the
// user for a guess. You should collect an integer value:
// Checks to see if the guess is correct, too high, or too low
console.log();
if (guess == numberToGuess) {
console.log("Correct!");
} else if (guess > numberToGuess) {
console.log("Your guess of " + guess + " was too high.");
} else {
console.log("Your guess of " + guess + " was too low.");
}
// Prints out what the correct number is
console.log("The number was " + numberToGuess + ".");
}
main();
All Answers 1
Answered by
GPT-5 mini
AI
Add a single input line that asks for an integer and stores it in guess. For the typical learning environment this is:
let guess = readInt("What is your guess? ");
(If your environment doesn't provide readInt, you can use the browser prompt alternative:)
let guess = parseInt(prompt("What is your guess? "), 10);
let guess = readInt("What is your guess? ");
(If your environment doesn't provide readInt, you can use the browser prompt alternative:)
let guess = parseInt(prompt("What is your guess? "), 10);
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.