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

do, while loop examples.. I'm struggling with..

First example

let number = 10;

do {
number = prompt("What time do you go to bed?");
} while ( number !== "10" );

alert("You can go to bed now!");

on line 4 does the variable get overwritten? Or does the variable store both 10 and line 4 into the same variable?

similar to the course question regarding do while loops (I'm struggling with that too (below)),

Second example

let secret;

do {
secret = prompt("What is the secret password?");
} while (secret !== "sesame");

alert("You know the secret password. Welcome!");

1 Answer

Juan Luna Ramirez
Juan Luna Ramirez
9,038 Points

You're right, the variable is overwritten.

So looking at your first example:

let number = 10;

do {
  number = prompt("What time do you go to bed?");
} while ( number !== "10" );

alert("You can go to bed now!");
  1. the variable number is declared and assigned the number 10
  2. the do statement is executed. Note that we are not checking the while condition yet. The do statement will always run at least one time since it ignores the while condition the first time. So it doesn't matter that the number 10 (which is the current value of the number variable) is NOT equal to the string "10" since, again, the while condition is ignored the first time. The do statement would still run at least once even if you had made the number variable equal to the string "10".
  3. In the first execution of the do statement, assign the string response (return value) from the prompt to the number variable. So yes, the value of the variable is overwritten.
  4. Now the while condition is checked. If the number variable, which is now equal to the response from running the do statement the first time, is not equal to "10" (resulting in a true statement), we run the do statement again. This keeps happening until the number variable (reassigned by the prompt response every time) is equal to "10" (resulting in a false statement), breaking out of the loop and triggering the alert.

Hope this didn't confuse you even more.

Thanks very much for your detailed explanation, it did really help :-)

Since then the let number = 10; gets overwritten I could omit = 10; and simply start by using:-

let number;

do { -code- } while ( -code-)

Juan Luna Ramirez
Juan Luna Ramirez
9,038 Points

Correct! It makes more sense to initialize the number variable as undefined.