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 trialJuan Luna
11,483 PointsProblem with a "do" loop.
Hi, I have to do a "do" loop that asks for a password and when the user type the correct answer, write a message in the document.
I have a problem that says that I need to call the "prompt" function just once inside the loop (just what I did, I think).
var secret = prompt("What is the secret password?");
var answer = false;
do {
secret = prompt("What is the secret password?");
if (secret === "sesame") {
answer = true;
}
} while (! answer)
document.write("You know the secret password. Welcome.");
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
3 Answers
Alexey Tseitlin
5,866 PointsChanged "var secret = prompt("What is the secret password?");"
var secret = "";
var answer = false;
do {
secret = prompt("What is the secret password?");
if (secret === "sesame") {
answer = true;
}
} while (! answer)
document.write("You know the secret password. Welcome.");
Roberto Cardenas
10,478 PointsYou have a lot of unnecessary code.
Everything you need can go inside the do while loop.
do { "your code here" } while ( "your code here" ) document.write("You know the secret password. Welcome.");
Ayoub AIT IKEN
12,314 PointsYou just have to remove the prompt before the "do" loop, at the top ;). cause its useless at that position