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 trialAgnes Caringal
6,239 Pointsits all about, do...while loop...
i cant pass with this challenge
var secret = prompt("What is the secret password?");
do {
document.write("You know the secret password. Welcome.");
} while ( secret !== "sesame" ) {
secret = prompt("What is the secret password?");
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
4 Answers
Kenneth Black
23,140 Pointsthey want the prompt in the "do" brackets and the password accepted after the "while" condition. The way you have it currently, it will say the password is correct regardless of the input, since the condition on the do while loop always happens at least once. Basically, swap your 4th and 7th lines.
Benjamin Laughman
7,026 PointsI was using this same code block, and stuck on it for several hours, with the correct 'do' and 'while' statements, but, it didn't take until I deleted that top variable statement, and just left it as var secret; That way it doesn't run two prompts before accepting your answer and displaying the "You know the secret password. Welcome."
Benjamin Laughman
7,026 PointsFor the record I was having trouble with this myself. Okay, so if you follow Mr. Black's advice, as I did, and swap your fourth and seventh lines, leaving all else the same, you get this:
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.");
The Challenge is going to count it as wrong, and say, "You only need to call the prompt() method once, and only inside the loop." I wasn't sure why this was coming up, so I put it all into Workspaces, and if you preview this block of code, it will ask you "What is the secret password?", and if you write "sesame" in the box, it will ask you "What is the secret password?" again. If you answer "sesame" a second time, it will then say, "You know the secret password. Welcome." So once I changed it to:
var secret;
do {
secret = prompt("What is the secret password?");
} while ( secret !== 'sesame' )
document.write("You know the secret password. Welcome.");
The program executed the prompt only once, and there was no problem with putting "sesame" in the first try and getting the Welcome response. It also will be accepted as correct by the Challenge in this format.
I believe that the idea is that you are just "declaring" the variable before the do...while statement, not actually ascribing any meaning to it outside of the function. That would make sense to me why it executed the prompt twice before accepting the word "sesame" as a correct answer. If you try it both ways in Workspaces it might make a whole lot more sense. Hope this helps.