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 trialJonathan Grieve
Treehouse Moderator 91,253 PointsQuestion about a while loop
Great start to this course by the way :) Learned a lot and reinforced stuff I already knew.
Now I'm showing my ignorance of while loops here I think.
But I worked my way through this code.
var secret = prompt("What is the secret password?");
while (secret !== "sesame") {
document.write("You know the secret password. Welcome.");
secret = prompt("What is the secret password?");
}
Which makes sense as the question asks you to give the program a way to ask the question again if the wrong text is typed in the prompt.
But doesn't this do the same even if the the right? "password" is given? Or am I missing something obvious?
Thanks. :)
var secret = prompt("What is the secret password?");
while (secret !== "sesame") {
document.write("You know the secret password. Welcome.");
secret = prompt("What is the secret password?");
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
2 Answers
Aurelio Decock
14,607 PointsAs far as I know
while (secret !== "sesame") {
document.write("You know the secret password. Welcome.");
secret = prompt("What is the secret password?");
}
says: while secret IS NOT sesame -> you know the secret password... and after that it opens a new prompt.
So I think the code should be:
while (secret !== "sesame") {
secret = prompt("What is the secret password?");
}
document.write("You know the secret password. Welcome.");
Because you jump out of the loop when the secret IS sesame
I'm only a beginner but this is what I think atleast
Micole Noddle
5,797 PointsDave McFarland, I tried Jonathan's code and it was the only code that passed. Aurelio's did not... if his is the correct code, why won't it pass this challenge?
Dave McFarland
Treehouse TeacherAurelio didn't post all of the code, just the changes he would make to Jonathan's code. The complete code should include the first prompt like this:
while (secret !== "sesame") {
secret = prompt("What is the secret password?");
}
document.write("You know the secret password. Welcome.");
But you're write Jonathan's shouldn't pass. I'll fix that.
Dave McFarland
Treehouse TeacherDave McFarland
Treehouse TeacherThat's right Aurelio Decock .
while
is a loop -- it will repeat over and over again. You don't want to print the message "What is the secret password" UNLESS the user provides the correct password -- that causes the loop to end. So place thedocument.write()
command AFTER the loop.Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsThanks Dave, So the code I used passed the challenge. Could this be a bug or is it okay for the example? Either way I now understand so thanks for your explanation. :)