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!
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
Nathaniel Sem5,870 Points
I'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..."
Jesse Schoonveld2,943 Points
The 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 Ogilvy3,142 Points
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 Sem5,870 Points
Ok I think I understand now...
So basically, it's saying: "While correctGuess(which is false) is true, perform the loop."
Thank you so much!