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 trialtylerpostuma2
5,175 PointsWhy is this do...while loop not working?
Hey everybody! So I was wondering what is wrong with this do...while loop? I set the variable secret outside of the loop, then the do should run that prompt everytime. Then if they guess it correct, they get it. Was just wondering why this wasn't working! Thanks!
var secret = "sesame";
do {
var secret = prompt("What is the secret password?");
} while (secret === "sesame") {
document.write("You know the secret password. Welcome.");
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
3 Answers
Jason Anders
Treehouse Moderator 145,860 PointsHey Tyler,
You're very close, except for a couple minor things.
You have the
secret
variable declared outside of the loop, which is correct, so you can't re-declare it inside the loop. To fix this, you just need to get rid of thevar
keyword. With it there, JS is trying to re-declare it... without it, JS is just using the variable (which is what you want).You want the loop (i.e. the question for password prompt) to run as long as the user is getting it wrong. Right now you are checking to see if the answer IS correct, which will keep the loop running as long as the user is getting it right (which is not what you want). So, you need to change you check to be "not equal".
Below is the corrected code for you to review. Have a look, and I hope it makes sense. And, just an extra note, to make the code more DRY, you don't need to assign a value to the secret
variable outside of the loop. You can just initialize an empty variable var secret;
The do loop
is assigning a value to secret
from the prompt and the while
is comparing them. Other that that... Good job!
var secret;
do {
secret = prompt("What is the secret password?");
} while (secret != "sesame") {
document.write("You know the secret password. Welcome.");
}
Keep Coding! :)
Steven Parker
231,269 PointsYour loop runs while the password is correct, and ends when it is wrong. You probably want it to go the other way.
Also, a few other things:
- You don't need to preset secret, it will be set by calling prompt.
- You don't want to declare secret again inside the loop, just assign it.
- You don't need those braces around the document.write.
- Semicolons after a statement at the end of a line are optional, but they are good practice.
var secret;
do {
secret = prompt("What is the secret password?");
} while (secret != "sesame");
document.write("You know the secret password. Welcome.");
tylerpostuma2
5,175 PointsThank you!