Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Brian Haucke
Brian Haucke
13,717 Points

Why do we need the ! in front of correctGuess

I'm a little confused as to why I need the ! in front of correctGuess in the while()?

If var correctGuess = false; wouldn't while (! correctGuess) make it true?

It seems like I should just be able to do while (correctGuess), but that gave me an infinite loop! :)

4 Answers

David Service
David Service
12,928 Points

Hi Brian,

From what I can tell it works like this:

The "not" logical operator looks for a "true"/"false" value from statement of the opposite of a set Boolean value (i.e. the set "false" value of the "correctGuess" variable has an opposite value of "true"), and this temporary "true" value allows the loop to continue running without actually changing the Boolean value of the "correctGuess" variable. All we're tell the "while ()" condition to do is take the opposite value (i.e. "true") of the current "false" variable value, and use it.

When the user guesses the correct number, and the value of the "correctGuess" variable is changed to "true", the "not" operator takes the opposite of that value (i.e. now "false" since the actual variable value is now "true") and uses it so that the loop stops.

This is simply a short-hand method of getting a "true" or "false" value from the opposite of a variable value that we set elsewhere in the program.

In my own workspace I simply used this to get the same result as the instructor:

do { guess = prompt("I am thinkg of a number between 1 and 10. What is it?"); guessCount += 1; if (parseInt(guess) === randomNumber) { correctGuess = true; } } while (correctGuess === false)

I think at the end of it all the above method works fine, but the instructor just wanted to introduce us to something new with the "not" operator.

Hope this helps, and good luck.

David

William Li
PLUS
William Li
Courses Plus Student 26,867 Points

Yes, that's the point here. (!correctGuess) is true.

In a do..while loop, while (condition) the condition needs to be true to keep the loop going.

(!correctGuess) is true, so the do while loop will keep going until you alter the boolean value of correctGuess.

var randomNumber = getRandomNumber(5);
var guess;
var guessCount = 0;
var notCorrectGuess = true;

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. What is it?');
  guessCount++;

  if (parseInt(guess) === randomNumber ) {
    notCorrectGuess = false;    
  }  

} while (notCorrectGuess);

Alternatively, you can use notCorrectGuess as variable, which is true until you guess the right number. For me it was easier to understand the concept this way.

Yes, this was way easier to understand. I wonder why this wasnt the example used...

R R
R R
1,570 Points

I have been stuck at this point of the tutorial for a while now and am still having trouble understanding the concept. No specific question here. Just venting. Will keep watching.