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 trialTyler Mauldin
1,468 PointsIssues with my while loop
I feel like I have a slight grasp on it but something is eluding me to why this script is taking so long to run?
var secret = prompt ("What is the secret password?");
var sesame
while ( sesame !== secret ) {
prompt = (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>
2 Answers
Braden Boaglio
9,375 PointsHello. The code below worked for me.
var secret = prompt("What is the secret password?");
while (secret !== 'sesame') {
secret = prompt();
}
document.write("You know the secret password. Welcome.");
Jason Anders
Treehouse Moderator 145,860 PointsWhile Gavin is correct, and his code is valid, the challenges are very picky and specific. If you do more than what they ask, it is highly likely it will not pass you.
The challenge did not ask you to create a new variable, and you are very close with the rest of your code. But you cannot assign something to "prompt" because that is not a variable. Inside the loop, all you have to do is reassign a new value to the "secret" variable:
var secret = prompt("What is the secret password?");
while (secret !== "sesame") {
secret = prompt("Wrong, try again!");
}
document.write("You know the secret password. Welcome.");
Hope that helps. Keep Coding! :)
Gav Cooper
4,261 PointsAh! I hadn't realised that this was a code challenge he was asking about, haha. It turns out I was just before that particular challenge in the Loops, Arrays and Objects course :)
Gav Cooper
4,261 PointsGav Cooper
4,261 PointsYour first line is fine. In line 2, you've declared a variable for what the correct password is, but you haven't actually said what string you want the password to be! if you want the password to be 'sesame' you could have something like
var sesame = "sesame";
In your while loop, your test condition doesn't yet make sense because JS doesn't know what the value of the sesame variable is, so it can't compare it to anything. On the next line down, you don't need an equals sign after your prompt, as prompt isn't a variable. Here, you could try
secret = prompt("That is the incorrect password. Try again");
Hope that helps.