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 trialViking Princess
999 PointsWhile loop exercise
I am stuck finishing loop exercises
var secret = prompt("What is the secret password?"); var password = prompt; var secret = "sesame";
while (password !== secret){ prompt("wrong password!"); password+=1; } document.write("You know the secret password. Welcome.");
I know it is wrong but can someone point at what am i doing wrong?
var secret = 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>
Thank you!
2 Answers
Erik Nuber
20,629 Pointsvar secret = prompt("What is the secret password?");
var password = prompt;
var secret = "sesame";
while (password !== secret){
prompt("wrong password!");
password+=1; }
document.write("You know the secret password. Welcome.");
There are a few issues.
You are assigning secret to both a prompt and then overriding that with "sesame" two lines later.
You are assigning password to the actual word prompt.
Within the while loop, you are just saying wrong password but not updating anything for the check so it will be an infinite loop.
you are also then setting password to be a counter. So this is a bit all over.
You do have the right idea just need to work on the execution. If you are looking for what is going on and how to do so please look at this. You can accomplish it any way you prefer doing this is just one way to do so.
var guess = prompt("what is the secret password?");
var tries = 1;
while (guess !== "sesame") {
guess = prompt("please try again, what is the secret password.");
tries += 1;
}
document.write("You know the secret password. Welcome.");
console.log(tries);
Here I start the tries counter at 1 because we are guessing for the password outside of the loop so it will always be at least one try.
the while loop is checking to see if the guess is not equal to "seasame" and will continue running as long as that condition is not met. This counts the number of times and, finally we print to screen and hide the number of tries by showing it in the console.
Viking Princess
999 Pointsokay i see it now .. Thanks a mil!