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!

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 Create a `do...while` loop

Jonathan Weinstein
Jonathan Weinstein
9,063 Points

Pretty stuck on this question

Could anyone give a clear explanation how this is supposed to work? My biggest problem is that i'm unclear about what comes after the 'do':

var secret = prompt("What is the secret password?");
return secret;

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

document.write("You know the secret password. Welcome.");

1 Answer

Hugo Paz
Hugo Paz
15,622 Points

Hi Jonathan,

Your loop is correct. What you have before is not.

Before the loop you just need to create the variable secret without assigning any value to it.

I dont know why you put a return statement there, it is used inside functions so you need to remove it as well.

Jonathan Weinstein
Jonathan Weinstein
9,063 Points

Thanks Hugo! That helped a lot. A quick question: What would happen if we assigned the variable INSIDE the loop? Why is it important to assign it outside the loop?