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!

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


confused with the while (! correctGuess)

but at the start var correctGuess = false;

so shouldn't }while ( ! correctGuess ) also be true? i.e the ! reverses the correctGuess (which was set to false as above initially)

which would terminate the loop after one guess, right or wrong?

or I'm missing something?

You gotta post the rest of the code man

3 Answers

The logic checks out β€”Β it's just a bit tricky β€” that loop will terminate as soon as guess is equal to randomNumber.

Since it's a do...while loop, it runs at least once no matter what. Then, it keeps running until guess is equal to randomNumber. guess will only equal randomNumber if the user guesses the random number. Once the user guesses the randomNumber, correctGuess is assigned the value true. As soon as that happens, the while loop's condition evaluates to false... because the opposite of correctGuess becomes false. Let me know if that doesn't make sense.

thanks i think i got it

Sorry first time asking, thought it will show up in the relevant execise section. Heres the code thanks.

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

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

do {
  guess = prompt('I am thinking of a number between 1 and 10. What is it?');
  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);
James Treehouse
James Treehouse
6,593 Points

So in your Do While loop you have to "do" the code inside at least once and after that you check the while condition. In your code your while condition is ! correctGuess. If that while condition is true then you continue to "loop" back through the code inside the "do". So, you are correct that while ! correctGuess should evaluate to true.

But it is incorrect to say that do while loop will terminate after 1 guess. If the guess is not === to the randomNumber then correctGuess will still be false since it will skip over that if(parseInt(guess) === randomNumber){...} statement. Only until the guesser guesses the randomNumber will the if(parseInt(guess) === randomNumber) evaluate to true which would make the code inside that if statement to run which would set correctGuess to true.

After the user finally guesses the correct guess then the while conditional ! correctGuess becomes ! true which is false thus ending the Do While loop.

"But it is incorrect to say that do while loop will terminate after 1 guess."

No one said that.

James Treehouse
James Treehouse
6,593 Points

Sorry, no one said that but I was just trying to show him that it was incorrect to say that because he asked "which would terminate the loop after one guess, right or wrong?"... Hope you didn't take offense in me trying to help.