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

Asad Sajon
PLUS
Asad Sajon
Courses Plus Student 918 Points

Something is wrong to understand........

Hi there,would you please help me to understand this?I have understood all of code without the three"guessCorrect"lines.

  1. var guessCorrect=false;
  2. guessCorrect=true; and
  3. ! guessCorrect Please see this

function getRandomNumber( upper ) { var num = Math.floor(Math.random() * upper) + 1; return num; } var randomNumber = getRandomNumber(10); var guess; var guessCount = 0; var guessCorrect=false;

do{ guess=prompt("i'm thinking a number between 1 and 9.What it is?") guessCount+=1; if(parseInt(guess)===randomNumber){ guessCorrect=true; }

}while(! guessCorrect)

document.write("<h1>You guessed the number</h1>"); document.write("it took "+guessCount+"tries to guess"+randomNumber);

1 Answer

Mohammed Khalil Ait Brahim
Mohammed Khalil Ait Brahim
9,539 Points

Ok! first things first

var guessCorrect = false;

This initialization is very logical since the user haven't entered any number meaning he didn't guess then his guess is incorrect or false!

Second :

  guessCorrect = true;

This statements gets executed only when the user's guess is a correct one! It kind of self explanatory.

Now for the last part which is the condition of the loop.

PS: in loops when the condition is true we continue looping otherwise we stop

PS2: when we put an exclamation mark before a statement this statement is negated meaning that if the statement evaluates to a truthy value then if you negate it it's gonna become a falsy one like :

!true = false
!false = true;

So Given the nature of our problem we keep looping <b>while the guess of the user is wrong</b> (This is symbolized when guessCorrect = false) and if we negate it then it becomes ... true that means we loop again