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 trialja5on
10,338 Pointsdo, 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
9,038 PointsYou'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!");
- the variable
number
is declared and assigned the number10
- the
do
statement is executed. Note that we are not checking thewhile
condition yet. Thedo
statement will always run at least one time since it ignores thewhile
condition the first time. So it doesn't matter that the number10
(which is the current value of thenumber
variable) is NOT equal to the string"10"
since, again, thewhile
condition is ignored the first time. Thedo
statement would still run at least once even if you had made thenumber
variable equal to the string"10"
. - In the first execution of the
do
statement, assign the string response (return value) from the prompt to thenumber
variable. So yes, the value of the variable is overwritten. -
Now the
while
condition is checked. If thenumber
variable, which is now equal to the response from running thedo
statement the first time, is not equal to "10" (resulting in a true statement), we run the do statement again. This keeps happening until thenumber
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.
ja5on
10,338 Pointsja5on
10,338 PointsThanks 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
9,038 PointsJuan Luna Ramirez
9,038 PointsCorrect! It makes more sense to initialize the
number
variable asundefined
.