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

Matt Roberts
Matt Roberts
1,888 Points

I don't get this at all.

Can I get some help here? No idea what they are asking here.

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

do ( secret === "sesame" );

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>

3 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hey Matt,

I suggest going back and watching the video on do...while loops, since Dave goes over exactly how to write a do while loop.

Here's a hint, though: you don't need to set the var secret. Try using a "flag" variable like the one Dave uses in the video.

Good luck! Let us know if you still have trouble.

Cheers :beers:

-Greg

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,075 Points

Ok, let me help you out!

A do while loop is a special type of while loop that will always loop Exactly Once! How does it do that? By performing the action within the do block before checking the looping condition in the while statement.

A do..while loop takes the form of this:

do {
}while();

Normally in a loop you place the code that you want to have repeated within the body of the loop, however, in this case, we place that code within the body of the do block like so:

var counter = 0;
do {
  counter++;
}while();

Then we place the conditions for our loop within the while statement like this:

var  counter = 0;
do {
  counter++;
}while(counter <= 10);

I recommend that you go back and watch the videos, as I don't think you have completely grasped how to use do..while loops yet. But hopefully me code above should have given you an idea of how to solve the problem.


If you haven't figured it out yet, here is your solution:

var  secret = "";
do {
  secret = prompt("What is the secret password?");
}while(secret !== "sesame");
Greg Kaleka
Greg Kaleka
39,021 Points

Just a nitpick, but a do...while loop doesn't "always loop exactly once" - it always loops at least once. :blush:

Dane Parchment
Dane Parchment
Treehouse Moderator 11,075 Points

True, from a mathematical standpoint you are correct!

Matt Roberts
Matt Roberts
1,888 Points

Yeah. I rewatched it and it makes total sense now. That's what I get for taking the test after a night's sleep in between watching the video. So much for dream retention. ;-)

Greg Kaleka
Greg Kaleka
39,021 Points

Glad you figured it out! Always helpful to go back to the last video when you're stuck.