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 trialEyad Hmoud
11,321 Pointswhat is the answer ?
I tried many time in this challenge but i couldn't get the answer
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.");
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
3 Answers
Jennifer Nordell
Treehouse TeacherI wish I knew what you tried before that's failing so I might explain where you might be going wrong. But I'll show you the code and then walk you through it.
var secret;
do {
secret = prompt("What is the secret password?");
} while(secret !== "sesame");
document.write("You know the secret password. Welcome.");
First we make a variable named secret without any value whatsoever. We can then guarantee that secret is not equal to "sesame" at this point. A do while loop will always run at least once. So on the first time it runs, it asks the user for the secret password. It sets secret equal to whatever the user typed in. It continues to do this until the value stored in secret is identical to "sesame". At that point, the loop exits and the welcome message is presented to the user.
Hope this helps!
Eyad Hmoud
11,321 PointsThank you so much Jennifer, i just got stuck with it for some time but i just got the answer correctly after many attempts, the problem was with the variable secret at first, i kept the prompt method in it.
Jonathan Grieve
Treehouse Moderator 91,253 PointsWhat you want to do is keep looping so you get the question untl the correct answer is typed.
You need to completely rewrite the code so you do something while a condition is met that will break out of the loop.
Something like this
variable = value;
do {}
while()