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

Anish Patel
Anish Patel
5,537 Points

Why is this not working ?

I made the do while loop but i am wondering why it will not let me pass the stage, it says declare the var secret outside the loop but i want the variable to constantly change until it says "sesame", so that it can then exit the loop

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

  document.write('Well done!');
<!DOCTYPE html>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
<script src="script.js"></script>

1 Answer

Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey Anish,

This challenge is looking for the following code:

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

It's better to declare the variable outside of the loop first, since if it was inside the loop you would be declaring the variable over and over, which is fairly messy.

This way, you declare your variable, then continually reuse the same version of the variable inside the loop.

Good luck with the course!

Anish Patel
Anish Patel
5,537 Points

Thanks for the help!