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

david Ramirez
PLUS
david Ramirez
Courses Plus Student 2,798 Points

i do not know if this correct

what i am i doing wrong?

script.js
do {
var secret = prompt("What is the secret password?");
}
while ( secret !== "sesame" ) {
  secret = prompt("correct");    
}
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>
Diogo Righi Barbosa
Diogo Righi Barbosa
5,023 Points

Hi there! This is not how the do ... while loop works. Check out on this link.

After the while, you just pass the condition! So, if the condition evaluates to TRUE, it will pass. If not, it will repeat!

Something like this:

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

alert('Correct!');
document.write("You know the secret password. Welcome.");

2 Answers

Kevin Faust
Kevin Faust
15,353 Points

hi. the syntax for a do while is:

do {
 //code
}  while (condition)

first you want to create the secret variable at the very top of your code. then you want to prompt the user for input inside your "do" loop and put their response into the secret variable. then do your condition checking in your while loop. try again before looking at the solution below

//









var secret;

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

document.write("You know the secret password. Welcome.");
Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello David,

You're very close. Here's what's wrong.

It's best to declare and initialize the secret variable outside of the loop. Also, you should only call prompt once in the do block of the do...while loop. You don't need a while block, as the while just states whether or not the do block should continue to be invoked. With these changes in place, you would end up with this.

var secret = '';

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

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

I hope this helps.