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 trialAlexandros S.
8,261 Points"Create a do while loop challenge" problem
So i'm having a problem with this challenge where i have to make a "do while" function that asks for a password. This is the code that i tried on my browser and works properly but i still get an error saying that i only need to call the prompt() method once.
var secret = prompt("What is the secret password?");
do {
secret = prompt("What is the secret password?");
} while ( secret !== "sesame")
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>
1 Answer
Jennifer Nordell
Treehouse TeacherIndeed, you do only need it call it once. You're calling it twice. You call it when initializing the variable secret and then again in the do while loop which is always guaranteed to run at least once. The trick here is not to initialize secret. all you need to do is change the top line to this:
var secret;
You could also do:
var secret = "";
Alexandros S.
8,261 PointsAlexandros S.
8,261 PointsYea it worked! Thanks a lot, this challenge was breaking my brain.