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

James O'Brien
James O'Brien
1,103 Points

I have no idea how a do...while loop would be any better here and don't know what to do.

As you can see, I have not the slightest clue what to even put in the do { } block. I don't understand.

script.js
var secret = prompt("What is the secret password?");
do {
  //I have no idea.
}
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>

2 Answers

Chase Marchione
Chase Marchione
155,055 Points

Hi James,

You'll want to declare the variable outside of the do while loop so that it is only declared once. Inside of the do clause, we'll have the prompt that will run as many times as necessary (until the user guesses the correct password of 'sesame'.) The condition for the loop is stated in the while statement. So, essentially, we're saying: do present the user with this prompt, and keep presenting them with this prompt, until they guess the proper password of 'sesame'.

var secret;

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

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

Hope this helps!

James O'Brien
James O'Brien
1,103 Points

I understand now! Thanks so much.