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 trialellie adam
26,377 Pointsstage 1 simplify repetitive task with loops
var secret = prompt("What is the secret password?"); while( seame === welcome){ document.write("You know the secret password. Welcome."); secret += 1; }
var secret = prompt("What is the secret password?");
while( seame === welcome){
document.write("You know the secret password. Welcome.");
secret += 1;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
2 Answers
Gavin Ralston
28,770 PointsOkay, so, first things first.
You've got most of the details written for you, a prompt which stores a user's password input, and the part where the document is updated telling the user they know the password.
All you really have to do is wrap that prompt (which gets the password try) in a while statement so it'll keep checking over and over til the user gets it right.
// set up a variable to hold the guesses
var user_guess = "";
// keep doing this til the user_guess equals "mypassword"
while (user_guess !== "mypassword" ) {
user_guess = prompt("Enter your password");
}
// once we're past the while loop, do whatever you want next!
That's the "outline" for the challenge. Using the names and variables given in the challenge, something just like this example should work just fine.
ellie adam
26,377 PointsThanks :)
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."); secret += 1;
this is working!
Gavin Ralston
28,770 PointsGreat!