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 trialDaniel Busch
6,997 Pointscode will not run correctly, While loop
Hi,
I have spent a lot of time trying to figure out this question with no success. Why is my code not running? Also, please post the correct code with this question (I have already tried several times before and am getting very frustrated). Thank you
var sesame == true; var secret = prompt("What is the secret password?"); while (secret !== sesame) { prompt("What is the secret password?"); } document.write("You know the secret password. Welcome.");
var sesame == true;
var secret = prompt("What is the secret password?");
while (secret !== sesame) {
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="app.js"></script>
</body>
</html>
2 Answers
Jon Kussmann
Courses Plus Student 7,254 PointsHi Daniel,
The idea is what you want to keep on asking the user for the password until the get it correct. In this case the password is "sesame". To make things simple, you want the string "sesame", not make a variable out of it.
If the user got it wrong, you want to ask the user again for the password and then assign that value to 'secret' so that it can be checked again in the while loop condition.
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.");
jessenagelberg
11,304 PointsOutside the while loop assign sesame to true using only one equals sign and see if that works.
Daniel Busch
6,997 PointsDaniel Busch
6,997 PointsThank you Jon, your explanation coupled with a visual example made this error clear to me. It makes more sense now.
Tommy Gebru
30,164 PointsTommy Gebru
30,164 Points