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

Loop challenge

I understand the concept but I dont understand what do they want me to print in the Do loop, it is not stated what they want. can someone explain to me what do they want me to print out in the do statement.

Thank you very much

script.js
var secret = prompt("What is the secret password?");

do {prompt ( "What is the secret password?");
   }
while ( secret !== "sesame" ) {
      prompt( " Please type in the correct 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>

4 Answers

Stephan Olsen
Stephan Olsen
6,650 Points

It wants you to do the same thing. All you must do is alter the code so it's a do...while loop instead of a while loop. Your code should look something like this:

var secret;
do {
    // do something here....
} while ( your condition )

Your actually close, just keep in mind that the while part of a do...while loop is only the condition and does not encapsulate any code. Furthermore you only need to declare the secret variable outside the loop.

Sorry Stephan Olsen , i still don't get you.

I understand that in the do statement it wants us to print out the prompt dialogue once and in the while condition it does not want any prompt dialogue.

Yet, when I do that, it still doesn't go through.

Thanks.

Stephan Olsen
Stephan Olsen
6,650 Points

What I mean is that it should look like this. After your while statement with your condition, you're opening a set of curly braces, which is not part of the syntax. The do...while loop ends after the condition.

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

Hi there Stephan Olsen , i got it,

Thank you for your patience and your prompt reply.

Really appreciate it. :)

Stephan Olsen
Stephan Olsen
6,650 Points

You're very much welcome :)