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

Kieran Corcoran
Kieran Corcoran
11,188 Points

(! correctGuess)

Hi,

I am really struggling to understand the concept of the while loop condition (! correctGuess)

In the video Dave explains that the ‘NOT’ operator turns true to false, and false to true which I understand. However, the variable correctGuess is set to false at the start of the programme so I take this to mean it should be changed to true in the while condition. Meaning the code should not loop, as the condition have changed from false to true using the ‘NOT’ operator.

Would anyone be able to explain this to me?

Many thanks

1 Answer

Steven Parker
Steven Parker
231,269 Points

It only changes to true when the number has been guessed. Until then it stays false, which means that "!correctGuess" is true, which means the loop keeps running.

Kieran Corcoran
Kieran Corcoran
11,188 Points

Steven, thank you. I have been over complicating this in my head for ages. Your simple explanation has just given me a light bulb moment.

Thanks Kieran