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 trialFroi Nunez
12,133 PointsWhile loop challenge
I'm not sure why it isn't working. It tells me " I should assign a new value to the secret variable inside the loop".
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
John Magee
Courses Plus Student 9,058 PointsRight now you test the input from the user once, you set the variable the first time they enter an attempt at the password. However you have no code to reassign the variable within the loop if they try again. Right now your loop will last forever because the value of secret will never change.
You don't even have to write more code at this time - you just have to refactor it a bit.
John Magee
Courses Plus Student 9,058 PointsFroi
Like I said earlier - you don't have to write any new code - you just have to refactor (move stuff around)
Think about the variable you are testing, and how that variable is set. Should it be set statically once or dynamically?
(Let me be clear, that I don't think just giving someone the answer when they have a problem on the code challenges does them any good)
Froi Nunez
12,133 PointsThanks!
Froi Nunez
12,133 PointsFroi Nunez
12,133 PointsI understand what you mean, but I'm having trouble actually applying the syntax. i have something like this now:
var secret = prompt("What is the secret password?"); var answer = false;
while (secret !== "sesame") { prompt("What is the secret password?"); if (secret === "sesame") { answer = true; alert("Correct password!"); } }
document.write("You know the secret password. Welcome.");
This still hasn't worked.
Froi Nunez
12,133 PointsFroi Nunez
12,133 PointsOOhh got it! that was way more simple than I thought. Thank you.