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

Below is the code for the last code challenge. This would work better as a do...while loop. Re-write the code using a do

I am stuck for a while I do not understand what am I suppose to edit the challenge Task 1 of 1.

Do I have to rewrite the code using a do without the conditional? I am confused...

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

8 Answers

Hugo Paz, your code works as of 12:15 PST 02/18/2015. GREAT JOB!

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

Wow. I was stuck here for an hour, not realizing I needed to declare the variable before defining it! Thanks for posting this. Any reason why defining the variable would not work in a program?

The right code is:

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

Hi Brian,

You are supposed to change the original code:

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

to something that uses a do...while loop

do(....){
//code here
}while(...)

Why are we supposed to be using a do...while loop when we haven't learned about do...while loops yet?

I can't get the above code to pass either. Totally stumped. Any other way to write it without a do...while loop?

I got it to pass with:

var secret;

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

error with red box said "Bummer! You only need to call the prompt() method once, and only inside the loop." Where can I put call the prompt() method once?

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.");

???

You are calling the prompt method once outside the loop, when you declare the variable secret. You just need to fix that and you are good to go.

no wonder, keep it simply in JavaScript sometimes gave me hard time without the expression. lol thanks.

So I removed the

secret = prompt("What is the secret password?");

from inside the 'do' section and this passed allowed me to pass the challenge. However, that means that there is nothing in the 'do' part of the statement...just in the 'while' section. Can someone explain why this works?

Could you post the whole code please?

var secret = prompt("What is the secret password?");
do {

}
while ( secret !== "sesame" ) {    
}
document.write("You know the secret password. Welcome.");

There seems to be a bug. That code is wrong. I will contact support.

Ok, thank you, Hugo. I couldn't understand why it worked. So, I'm trying several other things because that code was incorrect. However, for a few things I've done it has said it is correct, but now I don't know if it's just a bug or if it really is correct. Can you help me understand what kind of thing should be going in the 'do' section?

You should have something like this:

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

Basically the DO section contains the instructions to be run until the WHILE condition is met.

Ok, so the var secret doesn't need to equated to the prompt outside of the loop? I see that you just defined var secret but then the prompt is included inside the loop. That totally makes sense. I was getting hung up on trying to define var secret with the prompt. Thank you, Hugo. And thank you, Brian, for pointing out the {} with the while section!

I missed assigning the prompt value to the variable secret within the do loop.

Got it. That works! Thanks for all the help.

@Froi, I'm experiencing it now. Can't get it to pass. How did you end up getting it to work?

Micole, did you check your do while loop with the one posted above by Hugo? Can you paste the code you've been trying to run so we can see what's going on?

try remove the curly brace for while.

I tried it with a "do while" loop but I get an error that says " stick with just the while loop" Did anyone else experience this? I've had trouble getting it to work.

@Lindsay, I used the code below and it ended up passing, but I'm confused as to why it did since there are a lot of varying explanations on the forums...also this challenge is before we learn about do...while loops. That's the next video. If I used a do...while loop the code wouldn't pass, I kept getting an error saying NOT to use a do...while loop. Very confusing! Hugo's would not pass, because the challenge wouldn't accept a do...while loop.

```var secret = prompt("What is the secret password?");

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

This is weird. I just tried my code and it works. Your code on the other hand gives an error saying that you are using a loop, just not a do while loop.

Hmm...I wonder if this is a bug we might need to report, especially since it was accepting my earlier code and that was clearly incorrect.

I have no idea why I was getting errors. @lindsay, I definitely think it's a bug. Thank you Hugo Paz for contacting support! I don't know why do...while loops would even be in the challenge since they don't even show up until the next video. Oh well. Thanks for your help, guys! Really appreciate it!