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

I need help understanding the content on the do...while loop video.

In the video he made a variable named correctGuess and gave it a value of false, then in the do while loop he made a condition which changed the correctGuess value to true, if the condition was true. The thing I'm confused about is in the while () he put an ! in front of the correctGuess variable stating the loop will only run if the correctGuess isn't true. I don't understand this, he already made the statement not true when he gave it the value false at the beginning of the code. So with the ! wouldn't he be making it true. Without the ! wouldn't correctGuess stay false until the correct answer was entered in to the prompt, making it true and ending the loop. Wouldn't the ! change the false value to its opposite true and create a non stopping loop? I'm really confused because this ! seems like a contradiction. please help

var randomNumber= getRandomNumber(10);
var guessCount=0;
var guess;
var correctGuess= false;

function getRandomNumber(upper) {
var num= Math.floor(Math.random() * upper) + 1;
return num;}


do { 
guess=parseInt(prompt(I am think of a number bewteen 1 and 10, what is it'));
guessCount+=1;
if(guess===randomNumber) {correctGuess==true;}
while(!correctGuess)```

2 Answers

What's happening is the correctGuess is set to false. THEN, the while condition is set to ! correctGuess, meaning (while correctGuess is not false, not false means true). Keep in mind that a while loop will NEVER run if the condition is false, only when the condition is true will the while loop run.

The difference of the do...while loop is that the loop will always run at least once. So all the task inside the do{} will run first, THEN the while condition is tested. If the condition is true, the loop will run again until it is set to false.

So back to the example. the do loop will run and inside the do...while loop it has an if condition checking to see if the guess is equal to the random generated number. If so, change correctGuess to true.

Finally the while condition is checked. If the guess WAS equal to the random number, the correctGuess is set to true (false). So ! correctGuess is equal to false and will not run again.

I know its confusing but it gets easier once you use it a few times.

I hope this helps.

Thanks yeah that helps!

Hi Brandon - The '!' in the while condition doesn't change the value of correctGuess, it's just part of the condition for determining whether to continue executing the code in the loop. If you walk through the code step by step, here's what happens: correctGuess is initialized as false. The code in the 'do' block runs. If the guess is equal to the random number, correctGuess is set to True. The condition in 'while ()' is checked. If correctGuess was set to true, 'while (!correctGuess)' is false and the loop doesn't execute again. If correctGuess was not set to true, the condition is still true and the loop executes again, and continues to do so until the value of correctGuess changes to true. Does this help?