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

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,868 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.