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

nishnash
nishnash
6,267 Points

Don't get (! correctGuess)

<blockquote class="imgur-embed-pub" lang="en" data-id="a/26Vt3"><a href="//imgur.com/26Vt3"></a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>

trying to add a screen shot http://imgur.com/a/26Vt3

Line 8 - we assign correctGuess = False Line 13 - if guess = randomNumber --> correct guess is now 'True' ??? line 16 - if it is already true; why are we making it false again by ! correctGuess ?

i'm a bit confused with this... Does line 14 not replace var correctGuess to TRUE ?

He does a great job explaining the process but i'm just not getting the 'aha' moment here...

Does the while loop in line 16 only execute if the conditions are true in line 13??

3 Answers

nishnash
nishnash
6,267 Points

Getting it very very slowly but can feel an 'aha moment coming' :) <br> What the if statement does is reasssign the value of correctGuess if it's condition has been met. (so this changes correctGuess from false to true since the condition has been met...)

So, i think my confusion is more from the do while part of it... So the 'DO' runs the function at least once

the if within the 'DO', verifies if the conditions are met; if met, it moves forward....

In order for the loop to stop, the 'WHILE' needs to be false ( Thus changing the correctGuess from True to false --- since correctGuess was originally false, then the IF statement reassigned it to vale TRUE :::: so at this point, the WHILE needs it to be ! correctGuess in order to move forward..)

Thanks for the help...Sorry if this sounds confusing

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

What the if statement does is reasssign the value of correctGuess if it's condition has been met. So while the value of correctGuess is not a "falsey" value (and false of course is a falsey value) it won't increment at all. So the program ends when you make the right guess. And of course the program knows when you have guessed when it hits a "truthy" value or correctGuess. :-)

Hope this helps!

nishnash
nishnash
6,267 Points

ok, i think i got it :)

a few videos after that explains the part i wasn't getting... https://teamtreehouse.com/library/javascript-loops-arrays-and-objects/simplify-repetitive-tasks-with-loops/exiting-loops

Thanks again :)