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

Fareez Ahmed
Fareez Ahmed
12,728 Points

do/while loop.

Do I need an input variable? what's wrong here?

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

}
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
Fareez Ahmed
Fareez Ahmed
12,728 Points

I got it. I replaced:

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

with

secret;

in the do part.

2 Answers

Hey Fareez Ahmed,

You are very close with your code. You want to initialize the secret variable outside of the do...while loop as you are doing, but it only needs to prompt while inside of the do...while loop. So, calling on var secret; is enough to initialize it, and then you can prompt from within the loop like so:

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

If you're entering the correct code and not passing -- comment out the existing while loop at the start of the exercise. The instructions say to enter your code below the existing code but I couldn't get my code to pass until I commented out the the existing while loop. Was driving me crazy. If you delete the while loop and just enter you're do/while loop you also get the angry red x asking where the while loop went!