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 JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a `do...while` loop

Andrew Stokes
seal-mask
.a{fill-rule:evenodd;}techdegree
Andrew Stokes
Full Stack JavaScript Techdegree Student 1,723 Points

I am confused about how I can make this code a do while loop? I've tried everything I could think of. Please help:)

If you could explain as to how solved my problem that would be great too. Thank you.

2 Answers

Dave Harker
PLUS
Dave Harker
Courses Plus Student 15,510 Points

Hi Andrew,

You can scrap the initial user prompt and just declare the 'secret' variable only (so you can use it within the do/while).

Then put the loop into the do/while format by cutting the conditional (while) and putting it at the end instead of the beginning. Now you'll need to add the 'do' at the start of the loop to initiate it.

Just FYI - do/while loops will ALWAYS run at least once which is why we can cut out the initial user prompt as it will run within the loop.

In the end, you should end up with something like this:

var secret;
do {
  secret = prompt("What is the secret password?");    
} while ( secret !== "sesame" );
document.write("You know the secret password. Welcome.");

Best of luck,

Dave.