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 trialLeyton Parker
6,556 PointsWhile loop question
So I got the challenge right by some mix of luck and intuition.
I understand all of the code except why this is necessary (why do you need to reset the variable secret with the prompt?)
secret = prompt();
Please help :)
var secret = prompt("What is the secret password?");
while ( secret !== 'sesame' ) {
prompt( 'What is the secret password?' )
secret = prompt();
}
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
Errin E. Johnson
22,458 Pointsvar secret = prompt("What is the secret password?");
while (secret !== "sesame") {
secret += prompt();
}
document.write("You know the secret password. Welcome.");
John Miceli
3,151 PointsThanks for posting this. I too assumed the prompt command alone, within the while loop, would assign the newly entered value to the variable "secret". The additional statement within the loop is apparently necessary to update the variable "secret", replacing what was stored when the prompt is initially called as the variable is defined. Good "intuition", as I was stumped till I saw your posting.
Leyton Parker
6,556 Pointsso I'm not the only one who got a bit confused
Chyno Deluxe
16,936 PointsWhile technically your answer is correct, and by correct i mean it meets the requirements for the challenge to consider the answer passing. This is how I understood the question and how I answered it because it made most sense to me.
var secret;
while ( secret !== 'sesame' ) {
secret = prompt("What is the secret password?");
}
document.write("You know the secret password. Welcome.");
I hope this helps.
Leyton Parker
6,556 PointsThat was who I wrote it initially, but the challenge instructed me that I 'Needed' to replace the value for the var secret inside the loop so I wrote that extra bit
secret = prompt();
Chyno Deluxe
16,936 PointsWhile that might have been enough to pass the challenge, It would case an infinite loop and if the user didn't get the password correct the first time it would then prompt an empty prompt that would not update the secret variable.
Errin E. Johnson
22,458 Pointsvar secret;
do {
secret = prompt("What is the secret password?");
}
while ( secret !== "sesame" ) {
}
document.write("You know the secret password. Welcome.");
Errin E. Johnson
22,458 PointsD.R.Y. , declared the var secret before the loop, then I initialized the value within the do part of the loop, and there was no need to add code block for while part of the loop because the loop would end when the while condition hit false, the correct password.
Errin E. Johnson
22,458 PointsErrin E. Johnson
22,458 PointsI was going for D.R.Y. , not having to write out the statement again. I finally figured out when it was asking for new value to replace the initial, it was within the prompt(); only. Still this is not real clear on this, but I sort of get it. Lil intuitive with knowledge. :) How is my logic, moderator. :)