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 trialMatthew Cornell
1,304 PointsBut...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
12,397 PointsMaybe 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
8,424 PointsI 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.
john larson
16,594 PointsBrad, I really appreciate the discussion you brought to this topic.
Matthew Cornell
1,304 PointsRight, 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
12,397 PointsBasically, 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
2,138 PointsLaVaughn, "while (correctGuess === false)" in this particular situation is 1000 times more clear. They really need to change that.
Isabella Tan Hui Huang
3,577 PointsOh my god yes. I watched the explanation for !correctGuess 10 times and I still don't get it. This is definitely better.