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

Teacher Russell
Teacher Russell
16,873 Points

'do...while loops'

Can anyone please explain the effect of the boolean value here in a different or more thorough way? Can't wrap my head around how the while (! correctGuess) causes the document to write that you were correct, or.....anything about the partial while loop at the end .........what's going on?

2 Answers

That's the way do while loops work, the block of code in the do will be executed until the condition in the while is true. Once we make the right guess, we don't want to make any guesses so we manually change the flag from false to true, so when we check the condition again it returns false so the loop stops. Not the while(!correctGuess) causes the document to write that you were correct, but it just stops the loop from asking you for guesses once you made the correct guess.If you don't understand the "!" sign you could just write "while(correctGuess === false)" and it will work the same way. I believe you understand the concept now.

Teacher Russell
Teacher Russell
16,873 Points

It wasn't the boolean value I was confused about ( I actually instinctively get that), but the use of the while in this kind of loop. Your answer helped me understand that. Thanks a lot! It's usually a lot simpler than I'm trying to make it:)

Missy Kailet
Missy Kailet
4,475 Points

The following are the same:

while(correctGuess)
while(correctGuess === true)
while(!correctGuess === false)

And the following are the opposite of the above:

while(!correctGuess)
while(!correctGuess === true)
while(correctGuess === false)
  • Which means, "while correctGuess IS NOT true."

Which, in this example, while the player does not guess the right number, causing the variable correctGuess to remain false, execute the "ask the player to guess" code.

guess = prompt('guess the number'); //receives user input and stores in "guess"
//...
if( guess === randomNumber) //player guessed the randomly generated number
{ correctGuess = true } //change correctGuess to true, so that "while NOT TRUE correctGuess" is no longer a valid statement 
while(!correctGuess) //while NOT TRUE correctGuess, another way of saying while FALSE correctGuess

If you'd like a better explanation of why "while(correctGuess)" with no ! in front of it would evaluate to true, or is the same as "while(correctGuess === true)", feel free to read the following article.

https://www.sitepoint.com/javascript-truthy-falsy/

I hope that helped...

Teacher Russell
Teacher Russell
16,873 Points

Thanks, it wasn't the booleans I was having trouble with so much as misunderstanding the relationship between do and while used in do..while loops. I've got it now. At least I think I do:)