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 trialBrian van Vlymen
12,637 PointsBelow 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...
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
John Mutch
6,962 PointsHugo 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.");
Adam Laszlo Vincze
4,083 PointsThe right code is:
var secret = '';
do {
secret = prompt("What is the secret password?")
}
while ( secret !== "sesame" ) {
}
document.write("You know the secret password. Welcome.");
Hugo Paz
15,622 PointsHi 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(...)
Micole Noddle
5,797 PointsWhy 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?
James Maddox
17,154 PointsI 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.");
Brian van Vlymen
12,637 Pointserror 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.");
???
Hugo Paz
15,622 PointsYou 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.
Brian van Vlymen
12,637 Pointsno wonder, keep it simply in JavaScript sometimes gave me hard time without the expression. lol thanks.
Lindsay Groves
4,811 PointsSo 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?
Hugo Paz
15,622 PointsCould you post the whole code please?
Lindsay Groves
4,811 Pointsvar secret = prompt("What is the secret password?");
do {
}
while ( secret !== "sesame" ) {
}
document.write("You know the secret password. Welcome.");
Hugo Paz
15,622 PointsThere seems to be a bug. That code is wrong. I will contact support.
Lindsay Groves
4,811 PointsOk, 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?
Hugo Paz
15,622 PointsYou 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.
Lindsay Groves
4,811 PointsOk, 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!
Hugo Paz
15,622 PointsI missed assigning the prompt value to the variable secret within the do loop.
Lindsay Groves
4,811 PointsGot it. That works! Thanks for all the help.
Micole Noddle
5,797 Points@Froi, I'm experiencing it now. Can't get it to pass. How did you end up getting it to work?
Lindsay Groves
4,811 PointsMicole, 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?
Brian van Vlymen
12,637 Pointstry remove the curly brace for while.
Froi Nunez
12,133 PointsI 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.
Micole Noddle
5,797 Points@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?"); }```
Hugo Paz
15,622 PointsThis 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.
Lindsay Groves
4,811 PointsHmm...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.
Micole Noddle
5,797 PointsI 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!
Routine Poutine
26,050 PointsRoutine Poutine
26,050 PointsWow. 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?