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

excuse me, do ... while... loop why I can't pass check work

this is my code

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

script.js
var secret = prompt("What is the secret password?");
do {
  secret = prompt("What is the secret password?");    
}while ( secret === "sesame")
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,007 Points

You should only have one prompt statement (the one inside the loop). The top line should just be a variable declaration.

You'll also need to change the comparsion in the while statement. You want the loop to continue while the input is not the correct word.

Prateek Jain
PLUS
Prateek Jain
Courses Plus Student 1,797 Points

Hi,

a Do..While Loop works very similar to a while loop, just that it executes before checking the looping condition.

In your case, you are checking the condition whether secret is equal to sesame. Therefore, if that condition holds true, the loop will loop again.

What you do need to do is check whether secret is != sesame, and if that condition holds true ask for the password again.

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

Hope this helps.

Prateek Jain
Prateek Jain
Courses Plus Student 1,797 Points

And of course, as Steven mentioned, since you are using a do while loop. You only need to declare the variable secret and prompt for a password inside the loop.

thank you both I got it