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

! correctGuess logic

Hi I need some help with the logic of ( ! correctGuess ) in the problem:

First we declare correctGuess to false: var correctGuess = false;

Then in the do while we change correctGuess to true only when the user guess the correct number but the loop must keep going as long as correctGuess is false

do {
  guess = prompt('give me the number');
  guessCount += 1;
  if (parseInt(guess) === randomNumber) {
    correctGuess = true
  }
} while ( ! correctGuess )

For what I understand ! correctGuess changes the value from whatever it is to its opposite so in our case it should change correctGuess to true. So what I understand here is that "while ( ! correctGuess )" is saying "while correctGuess is true" because its changing the previously false declaration to true.

Obviously in the practice the loop runs as long as the user doesn't guess the value so correctGuess is false but I can't see the logic behind it.

2 Answers

Emil Rais
Emil Rais
26,873 Points

! is known as "negation" or "not". !false is true and !true is false. "while ( ! correctGuess )" is saying: "while correctGuess is not true". "while ( correctGuess )" is saying: "while correctGuess is true".

Thanks Emil now I understood

Emil Rais
Emil Rais
26,873 Points

I'm happy to hear that. Merry coding.

Shawn Manning
Shawn Manning
2,775 Points

This was a much better explanation than the video. Thank you.

Nick Brigham
Nick Brigham
2,138 Points

This is confusing because correctGuess is set to false (at least to me). As pointed out in another answer, it should really be changed to "while (correctGuess === false)" because it is astronomically clearer.

Emil Rais
Emil Rais
26,873 Points

Nick Brigham - While "while ( correctGuess === false )" might initially seem clearer, I would argue that it is not and at the same time it is also very verbose. You will see the "while ( ! correctGuess )" way of presenting a conditional so often (it is the most common) that it will become second nature for you to read. It also reads better: "As long as we haven't had a correct guess, do: ..." whereas the other way of putting it would read more in terms of "As long as correctGuess is false" leaving you to do some mental gymnastics to figure out what that actually means.

Nick Brigham
Nick Brigham
2,138 Points

100% respect that. I would read it as "while correctGuess is still false". and while I use the "!correctGuess" format every single time, I would argue that the long-winded explanation (without a visual) made it difficult to understand, and that because of this specific program, it made it more difficult to understand as well. In most other contexts, this is a rather basic concept to teach/learn. ! preceding means "not" (as you said originally). like when changing lower case letters in a string to upper case, to check if it isn't an alphabetical character, you could just do " if ( !isalpha[current character])" (this is in C language). To me, the context of my example is a much easier way to grasp it. Granted, this could just be me.

Jonah Shi
Jonah Shi
10,140 Points

I still don't understand, as correctGuess is set to false in the first place. !correctGuess should saying " while correctGuess is true" instead.

Emil Rais
Emil Rais
26,873 Points

The value of correctGuess does not change the meaning of the condition. What the value of correctGuess does is determine whether or not the condition is satisfied.

it's false until it is guessed correctly. The loop will continue as long as it is NOT guessed correctly. When I see: while (!correctGuess) ; In my mind I'm hearing, while the answer is NOT true...keep the loop going.