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 trialColin Stell
Front End Web Development Techdegree Graduate 25,702 PointsHelp with this JavaScript Code Challenge.
Struggling to figure out how to do this 'do...while' loop. Maybe my brain is fried but I can't figure out what to put in do {}.
var secret = prompt("What is the secret password?");
do {
} while ( secret !== "sesame" ) {
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="script.js"></script>
</body>
</html>
3 Answers
Steven Parker
231,269 PointsThe "do" is in the right place.
But the code block that previously came after the while
now goes in between the do
and the while
. The conditional expression after while
is now the last part of the loop, so you can put a semicolon after it.
Then, before the loop, just declare secret but don't call prompt to initialize it. Let that happen in the loop.
I'll bet you can get it now without an explicit code spoiler.
Carrie Short
3,529 PointsRight now you're combining the syntax of a do while loop and a while loop. Move the prompt into the do and end with the while conditional.
do {
secret = prompt("What is the secret password?");
} while ( secret !== "sesame" )
Colin Stell
Front End Web Development Techdegree Graduate 25,702 PointsGotcha, thanks for the help! I was able to figure it out.