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

Why is this not working?

The error says Bummer: You only need to call the prompt() method once, and only inside the loop. Hint: Declare the secret variable before the loop.

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

} while (secret !== "sesame")

document.write("You know the secret password. Welcome.");
<!DOCTYPE html>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
<script src="script.js"></script>

1 Answer

Valerio Ripamonti
Valerio Ripamonti
5,022 Points

The prompt method returns what's typed by the user (the password), you can store it the variable 'secret'. I've declared the variable secret outside the loop, in this way the variable is declared (created) only once. Using a do-while loop a prompt will appear at least once and will always appear until the password is correct.

var secret;

do {

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

} while ( secret !== "sesame" )

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

Have Fun and happy coding!