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 A Closer Look at Loop Conditions

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

elaborate plz.. why use 'guess' variable inside while loop with the same value as 'randomNumber' at the start of code

var randomNumber = getRandomNumber(upper);
var guess;
while (guess !== randomNumber){
guess = getRandomNumber( upper );
  attempts+=1;
}

3 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Saqib,

The idea is that the computer chooses a random number, and then has to guess it. The loop will repeat while your guess does not equal the random number. The way a while loop works is the code will keep looping as long as the condition is met. In this case, the condition is guess !== randomNumber, so after each loop we check to see if the condition is met (the guess was wrong), and if so we loop again. If the condition is not met (the guess was equal to the random number), then we exit the loop.

It might look like the guess is the same as the original random number, but it's not. You're running the same code to generate a random number, but most of the time the random number will be different each time.

Hope that helps!

Cheers :beers:

-Greg

pat barosy
pat barosy
6,759 Points

Hi Greg, Am I correct in assuming that what you're saying is that every time I see getRandomNumber(upper), the computer is running that as a called function & coming up with a new random number? Also, if I am correct, why is guess initially assigned as an empty variable. Why isn't it set to zero, as in zero guesses yet? Thank you

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

oh my days i just got it... watching the video for the 3rd time, and ur 2nd part of the answer helped! "guess" was empty at 1st so it was not equal and loop RAN then in the code block we used "guess" variable and gave it same value as "randomNumber " to generate another number one so we can check if that met the condition in the loop and run the loop again or not n so on.......

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

this is the thing about javascript that annoys me a bit, but then enjoy it when u get it ! things so little when u dont get it complicate the whole programme and u think to urself whyyy dont i get it...is it right course for me:/

Greg Kaleka
Greg Kaleka
39,021 Points

It's not just Javascript - all programming is the process of solving problems. As you progress, you'll be solving harder and harder problems. If you can embrace the frustration because you love the feeling when you crack the problem, then programming will be very rewarding for you!

Good luck!