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 trialHui Zheng
Courses Plus Student 1,380 PointsThe While Loop
So initially when the program started, the value of guess was set to be zero. Then if the value of guess does not match the value of randomNumber, the block of code inside the while loop gets executed. So now the value of guess gets changed to the value of randomNumber because of this statement --> guess = getRandomNumber(upper); . Then the new value of guess gets compared with the next generated value of randomNumber again in the next iteration until the two values match thats when the loop stops because the condition has become false. In each iteration, the attempt is incremented by 1. I am not sure if my understanding in the mechanics of this code is correct.
4 Answers
KRIS NIKOLAISEN
54,972 PointsYour statement with some corrections:
So initially when the program started, the value of guess was undefined. Then if the value of guess does not match the value of randomNumber, the block of code inside the while loop gets executed. So now the value of guess gets changed to a new random number because of this statement --> guess = getRandomNumber(upper); . Then the new value of guess gets compared with the unchanged value of randomNumber again in the next iteration until the two values match thats when the loop stops because the condition has become false. In each iteration, the attempt is incremented by 1.
KRIS NIKOLAISEN
54,972 PointsrandomNumber is assigned the result of the function at the beginning of the code after which it does not change. There would have to be another assignment to do so.
And yes a variable declared without being assigned a value is undefined.
You can check these by logging the values to the console
Hui Zheng
Courses Plus Student 1,380 PointsLet's say hypothetically the value of the randomNumber is 2200 at the beginning. So if it stays unchanged then it will continue to stay as 2200 throughout each iteration??? So when the loop enters the second iteration or on the second attempt of the guess, the value of randomNumber will continue to remain as 2200. Then going to the third iteration of the loop and third attempt of the guess, the value of randomNumber still remains as 2200. What I am trying to understand here is that when you stated the value of randomNumber does not change, do you imply that the value of randomNumber stays constant throughout each iteratiion/attempt? Thanks
KRIS NIKOLAISEN
54,972 PointsYes. Only guess changes on each iteration. randomNumber stays the same. Here is a snapshot with a console.log if you want to see it in action.
Hui Zheng
Courses Plus Student 1,380 PointsAt the beginning we passed in the value 10000 of variable upper as a parameter to the function, the function returned a result after it performed the calculation on the formula Math.floor( Math.random() * upper ) + 1;. Lets say hypothetically the random number (Math.random()) generated within the formula was 0.3856 and the result produced by the formula was 4210 from the get go. These two numbers stayed constant later on when we used the result 4210 in comparison to the value of guess as the test condition. In terms of the value of guess, since we declared it without a value, so it was set to be undefined at the beginning. Every time the condition inside the while loop is true, the block of code within the curly braces get executed. Inside the block of code, we changed the value of guess to the value of getRandomNumber(upper). However, the value of getRandomNumber(upper) here is not same as 4210 the random number originally created at the beginning. The Math.random() function within the formula generates a different number in each iteration when the test condition is true, as a result the result of the formula is different in each iteration. Then we use this different value to compare with the original fixed value of getRandomNumber in each iteration. Not sure if I genuinely understand this time.
KRIS NIKOLAISEN
54,972 PointsI think the confusion is that randomNumber is not assigned the function itself, it is assigned the result of the function at the point of assignment. It is just a variable that has been assigned a number. The first iteration compares undefined to an integer so the loop will execute at least once.
Try this code:
x = 5;
y = x + 5;
console.log(y);
x = 6;
console.log(y);
y = x + 5;
console.log(y);
The result is:
10
10
11
y doesn't change until it is reassigned
Hui Zheng
Courses Plus Student 1,380 PointsThanks professor. I understand it now.
Hui Zheng
Courses Plus Student 1,380 PointsHui Zheng
Courses Plus Student 1,380 PointsSo the value of the randomNumber variable does not get changed in each iteration? I thought the formula Math.floor(Math.random() * upper) + 1 generates a random between within a specific range in each iteration since the Math.random() function was incorporated to the formula. By the way thanks for the help Professor. Also, when a variable is declared without a value, the default value will always be set as undefined and not zero??