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 trialFuad Muhammad
4,273 PointsWhat is mean "(! correctGuess)" on the do..while loop?
Please explain me about the point on the (! correctGuess) on the do...while loop. For example, if we see this code;
var correctGuess = false do { //something if (condition) { correctGuess = true; } } while ( ! correctGuess) <----- Who goal from this statement? Is first correctGuess(=false)? or correctGuess(=true) on the condition?
1 Answer
Tom Lawrence
8,685 Pointsa do while loop runs when the stuff inside the while part is true (but always once whatever), The stuff inside the while part is evaluated into a bool for example:
while (1 === 1)
the first thing that happens is the 1 ===1 is evaluated as a bool, 1 is equal to 1, so it is true, so behind the scenes it is converted into:
while(true)
so it then runs based on the true bool value, and re-evaluates it each loop till its false (which it wouldnt ever be in this case)
If for example you then had:
while(correctAnswer)
correctAnsweris already a bool, it does it based on its value, if true it runs, otherwise it wont. You could if you wanted also write while(correctAnswer == true) but no need.
! is a NOT operator, it basically flips everything, so if I wrote:
while(!correctAnswer)
if correctAnswer was true, correctAnswer would be flipped to false and not run
while(!correctAnswer)
if correctAnswer was false, correctAnswer would be flipped to true and run
You can think of it as saying "while correct answer is not true",