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

Colin Stell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Colin Stell
Front End Web Development Techdegree Graduate 25,702 Points

Help with this JavaScript Code Challenge.

Struggling to figure out how to do this 'do...while' loop. Maybe my brain is fried but I can't figure out what to put in do {}.

script.js
var secret = prompt("What is the secret password?");
do {

} while ( secret !== "sesame" ) {
  secret = prompt("What is the secret password?");    
}
document.write("You know the secret password. Welcome.");
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

3 Answers

Steven Parker
Steven Parker
231,269 Points

The "do" is in the right place.

But the code block that previously came after the while now goes in between the do and the while. The conditional expression after while is now the last part of the loop, so you can put a semicolon after it.

Then, before the loop, just declare secret but don't call prompt to initialize it. Let that happen in the loop.

I'll bet you can get it now without an explicit code spoiler.

Right now you're combining the syntax of a do while loop and a while loop. Move the prompt into the do and end with the while conditional.

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