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
Paula Wills
2,893 PointsDon't understand the error message I am getting.
Assignment: This is the code we used in the last code challenge. After learning about do...while loops, don't you think this would work better in the do...while style? Re-write the code to use a do...while loop.
Bummer! You only need to call the prompt() method once, and only inside the loop. Hint: Declare the secret variable before the loop. Below is my answer.
var secret = prompt("What is the secret password?");
do { secret = prompt("What is the secret password?"); if (secret === "sesame") { document.write("You know the secret password. Welcome."); } }while ( secret !== "sesame" )
document.write("incorrect");
3 Answers
David Bath
25,940 PointsThe hint says to declare the variable before the loop. Okay, so to declare a variable you just need to do
var secret;
Then the rest of your code should work fine (though I don't know what that last document.write is for. I don't think you need it).
Ricardo Hill-Henry
38,443 PointsBy declaring the variable secret to hold the value of a prompt, you'd essentially be forcing someone to type "sesame" twice in order to be successful here. The interpreter will run that command the moment it sees, forcing the user to enter "sesame." As the interpreter continues, it'll run into another prompt before ending (assuming the right value was entered), making another prompt emerge. This is why your secret variable should be declared with no value (or at least not a prompt).
Paula Wills
2,893 PointsThank you for the explanation. I thought I had already declared it, and didn't realize they were asking me to remove the prompt.
Paula Wills
2,893 PointsThat did it!!! Thank you!
Paula Wills
2,893 PointsPaula Wills
2,893 PointsThank you for spelling it out for me. I thought I had already declared it, and didn't realize they were asking me to remove the prompt in the var.