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 trialNathaniel Sem
5,870 PointsI'm finding it hard to understand his code.....
The " while ( ! correctGuess ) " statement really confuses me here.
Doesn't this make correctGuess = true, since the not-operator reverses the Boolean value of the correctGuess variable?
If not, does this mean that "while (! correctGuess)" basically means "While correct guess is not false..."
3 Answers
Jesse Schoonveld
2,943 PointsThe while(! correctGuess) makes it so the above do loop keeps getting looped while the correctGuess boolean is false. When the player guesses correctly the loop is done and it goes onwards to the part where it tells the player they entered the correct number and how much tries it took.
The do loop is being run as long as the while condition is met. Since the correctGuess boolean is set to false at the start of the script it keeps looping every time until it is set to true.
Audra Ogilvy
3,142 PointsI had the same confusion. We weren't given enough information earlier in the JavaScripts Basics course, I think. I'm not positive, but I'm guessing that when a variable (x) is inside of conditional parentheses, all on it's own, that's like saying "if x is true" or "while x is true". If I'm right, that would mean that, regardless of what the variable has been changed to:
if (x) means "if x is true" and if (!x) means "if x is false" and while (x) means "while x is true" and while (!x) means "while x is false"
What was confusing for me was that Dave says that the not operator (!) inverts a Boolean value to its opposite. But because I didn't know that if(x) meant "if x is true", I assumed that while (! correctGuess) was taking whatever value was in the variable and inverting it. That would mean that if guess = randomNumber (the guess was correct), then the variable correctGuess would be changed to true (in the if code block inside the do while code block) and then (! correctGuess would be false (the inversion of true), which would be the opposite of what we wanted.
Sooooo, that's why I'm guessing that the if and while conditional statements DON'T CHANGE the variable - i.e. they don't change the contents of "the box", and if a variable is inside their parentheses all on its own, that means "if the variable is true" and that conditional question has nothing to do with what the Boolean value has changed to.
Does that sound right?
Nathaniel Sem
5,870 PointsOk I think I understand now...
So basically, it's saying: "While correctGuess(which is false) is true, perform the loop."
Thank you so much!
Jesse Schoonveld
2,943 PointsI'm sorry i completely misinformed you, i edited my answer to be correct. Please reread it and if it helps you out upvote it :) But from your response it seems like you got it yourself already anyway.
PS: If you respond to answers you should use the comments, not add an answer.