Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTerry Babyuk
9,231 PointsGuess a number game using "while loop"
Hi guys,
I'm hoping someone can shed some light on this. I'm just practicing my loops with a game that asks the user to guess a random number between 1 and 10. When the number is guessed, they get an alert saying "You guessed correctly!". The problem is, my program seems to run indefinitely no matter how long I try to guess the numbers. It's almost as if it's not registering the correct number. What did I do wrong with my code?
const randomNumber = getRandomNumber(10);
function getRandomNumber(upper) {
return Math.floor( Math.random() * upper ) + 1;
}
let guess;
while (parseInt(guess) !== randomNumber) {
prompt("Guess a number between 1 and 10:");
}
alert("You guessed correctly!");
1 Answer
Terry Babyuk
9,231 PointsThanks Adam! I had a sense it was a simple fix, but my brain wasn't working well yesterday. Much appreciated!
Adam Pearson
19,432 PointsAdam Pearson
19,432 PointsSimple fix, you're just not assigning the user input from the prompt to the guess variable!
guess = prompt("Guess a number between 1 and 10:");