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

Matthew Cornell
Matthew Cornell
1,304 Points

But...why?

This seems totally backwards to me. Why would you set correctGuess = false, and then changing the correctGuess variable to true, and state your while condition as:

while( ! correctGuess)

Why not jut start the correctGuess = true, change it to false when the user guess = randomNumber and set your while ( correctGuess)

In essence, why use the ! operator at all?

4 Answers

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

Maybe the name of the variable is confusing you. It's longer and not as pretty but you could also write it as

while(!correctGuessHasBeenSubmitted)

We start with a false value because the user has not made the condition true by guessing correctly. The same as if you had a quiz you would not start by assuming that all of the answers are correct. In the default state (no answer submitted) they are incorrect.

To be clear, you COULD do it backwards as you are suggesting and it would still work fine.

while(guessIsWrong)

Then you could drop the !

Brad Penney
Brad Penney
8,424 Points

I believe that the reason this section is so unclear is because it was necessary to contrive an example of a do...while loop. While loops are used all the time and are easily understood by individuals and teams; do...while loops are not nearly as common and only used as a last resort. So contriving an unrealistic and somewhat muddled example was necessary to show a rather unused (and rarely resorted to) part of JavaScript. After doing many programming courses in several different languages, I've yet to see a really good reason to use a do...while loop.

Brad, I really appreciate the discussion you brought to this topic.

Matthew Cornell
Matthew Cornell
1,304 Points

Right, so just rename the variable to incorrectGuess and set it to true... but that would be confusing too. Basically you are saying it is for consistency with the names chosen for the variables.

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

Basically, yes. If you are working on a project solo then you can do whatever makes the most sense for you. If you work on projects with a team you are going to want to also be conscious of what's going to make sense for them as well. Maybe it means using more descriptive variable names if that makes it more clear.

correctGuessHasBeenSubmitted = false might be a little less ambiguous than !correctGuess

Maybe even drop the ! and go with

while( correctGuess === false)

Being clear is much more important than keeping it short

Nick Brigham
Nick Brigham
2,138 Points

LaVaughn, "while (correctGuess === false)" in this particular situation is 1000 times more clear. They really need to change that.

Oh my god yes. I watched the explanation for !correctGuess 10 times and I still don't get it. This is definitely better.