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 trialDaniel Morel
2,957 Pointswhile(! correctGuess) ?
I get the whole process of making a true or false flag and making the variable.
I don't get why the ! correctGuess and all the rules implicated to it.. can someone explain?
3 Answers
Steven Parker
231,275 PointsAs michael guessed, the "!" symbol is the logical "NOT" operator. So "while (!correctGuess)
" means "keep looping if we still do not have a correct guess". When the value of "correctGuess" is set to "true" inside the loop, the loop stops repeating.
Garrosh HellScream
7,503 Pointswhile (!correctGuess) is confusing. Whats wrong with using while (correctGuess == false)? It means the exact same thing right? Matter of preference?
Steven Parker
231,275 PointsSure, if you find that that easier to read, it is a functional equivalent. But it is not as efficient to execute.
Daniel Morel
2,957 PointsWant to thank everyone for the help and the correct guidance!.
Matt Brock
28,330 PointsYep, michaelcodes is correct. the !
, is called the Logical Not Operator (sometimes called a "bang"). In your code, the while
loop will run until its condition returns false
, i.e. until "NOT correctGuess" is true, or the guess is incorrect. Here's the Mozilla Dev Network page on Logical Operators: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators.
michaelcodes
5,604 Pointsmichaelcodes
5,604 PointsI don't have much JS experience but in most languages ! means not. So this would be saying while NOT correct guess