JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Hui Zheng
PRO
Hui Zheng
Pro Student 1,233 Points

The 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
PRO
KRIS NIKOLAISEN
Pro Student 45,971 Points

Your 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.

Hui Zheng
Hui Zheng
Pro Student 1,233 Points

So 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??

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 45,971 Points

randomNumber 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
Hui Zheng
Pro Student 1,233 Points

Let'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
PRO
KRIS NIKOLAISEN
Pro Student 45,971 Points

Yes. 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
Hui Zheng
Pro Student 1,233 Points

At 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
PRO
KRIS NIKOLAISEN
Pro Student 45,971 Points

I 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
Hui Zheng
Pro Student 1,233 Points

Thanks professor. I understand it now.