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 trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

It's impossible for me to guess the correct answer and the loop won't stop. Can anyone help?

var randomNumber = getRandomNumber(10); var guess; var guessCount = 0; var correctGuess = false;

function getRandomNumber( upper ) { var num = Math.floor(Math.random() * upper) + 1; return num; }

do { guess = prompt("I am thinking of a number between 1 and 10. Can you guess the number?"); guessCount += 1; if (parseInt(guess) === randomNumber){ correctGuess === true; } } while ( ! correctGuess ) document.write ('<h1>You guessed the number!</h1>'); document.write ('It took you ' + guessCount + ' tries to guess the number ' + randomNumber);

This code should match the teachers code in the video.

2 Answers

Hey Justus Thompson,

The only problem with your code is that in your if statement in the do-while loop, you were using the === comparison operator to try to assign a value to correctGuess. Any time you are assigning a value to a variable you must always use the single = sign. == or === are reserved for comparisons only!

Here is the fixed code! :)

var randomNumber = getRandomNumber(10); 
var guess; 
var guessCount = 0; 
var correctGuess = false;

function getRandomNumber( upper ) { 
var num = Math.floor(Math.random() * upper) + 1; 
return num; 
}

do { 
guess = prompt("I am thinking of a number between 1 and 10. Can you guess the number?"); 
guessCount += 1; 
 if (parseInt(guess) === randomNumber){ 
//Changed to assignment operator, single = sign
  correctGuess = true; 
 }
} while ( ! correctGuess ) 
document.write ('You guessed the number!'); 
document.write ('It took you ' + guessCount + ' tries to guess the number ' + randomNumber);

One way is this:

JSFIddle Link <- This is just an uprgrade to your code check the answer below.

The problem your code doesn't stop is this line:" correctGuess === true; "

This means you are comparing the correctGuess to boolean value. Thats why your loop doesn't end.

You have to set the variable correctGuess to true, this looks like this:" correctGuess = true; "

Try this and I hope it helps.

Nejc, I literally said that above so what is the purpose in answering a question with the same answer that's already been answered? lol Btw, changing the "document.write" to "alert" is not an upgrade of any kind. lol

Well the jsfiddle complains about the document.write method if you don't know. And I'm on a SPS course and typing as much as I can. Took me longer to write and didn't refresh the page every 2secs.

It's an example, so using document.write is fine. This code isn't going to be used by thousands of people on a day to day basis so I think we can let Justus get away with using document.write in his example. What do you say? lol