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 trialNathan Broaddus
5,875 PointsWhy doesn't this work?
This seems logical to me. Why doesnt this work?
if the "var secret" is going to have the exact same value within the loop, then why do I have to assign it a value in order for the loop to work?
var secret = prompt("What is the secret password?");
var pass = "sesame";
while (secret !== pass ){
secret;
}
document.write("You know the secret password. Welcome.");
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
4 Answers
rydavim
18,814 PointsThe idea of the while
loop in this challenge is to give the user several opportunities to guess the value of pass
. You need to update the value of secret
inside the while
loop, otherwise the loop will run forever if the user doesn't guess the password correctly the first time.
while (secret !== pass ){ // If the user's guess is NOT the same as the password...
secret; // You need to let them guess again here.
}
Hopefully that helps to clarify the challenge. Please let me know if it still doesn't make sense. Happy coding! :)
Robert Pazo
8,884 PointsBy assigning a value to secret before the while loop and no way of updating secret in the while loop, the value assigned to secret remains the same as the initial returned value. Even though it seems as though you assigned the prompt function to secret, you actually assigned prompts return value to secret, which is why it will not display the prompt after the while loop begins. By placing secret = prompt("What is the secret password?"); inside the while loop you keep reassigning the variable to the value which the prompt function will return. Hope this helps :)
Chyno Deluxe
16,936 PointsYou code COULD work but you would need to swap the place of the prompt to inside the while loop.
var secret;
var pass = "sesame";
while (secret !== pass ){
secret = prompt("What is the secret password?");
}
document.write("You know the secret password. Welcome.");
Nathan Broaddus
5,875 Pointsthis may seem dumb, but to me it looks as if the prompt is inside the while loop.
Chyno Deluxe
16,936 PointsThat's because it is inside the while loop.
Nathan Broaddus
5,875 Pointsthen your first answer doesn't make any sense to me.
Chyno Deluxe
16,936 PointsThe variable secret will hold no value by default making the while condition equal to true and thus will run the prompt asking the user for the secret password.
It will continue to run until secret is equal to "sesame"