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 trialMoises Miguel
5,028 PointsBelow is the code for the last code challenge. This would work better as a do...while loop. Re-write the code using a do
I need help figuring this one out
var secret = prompt("What is the secret password?");
do {
if (secret === "sesame") {
document.write("You know the secret password. Welcome");
}
} while (secret)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
1 Answer
Colin Bell
29,679 Points- You need to declare the
secret
variable in the global scope, but you don't need to set it to the prompt until thedo
section. - You want the
document.write()
after the do/while loop has completed. - You need to have an expression for your
while
to test.
var secret; // Declare this here so it's accessible in the global scope
do {
secret = prompt("What is the secret password?"); // Keep doing this
}
while ( secret !== "sesame" ); // While secret does not equal "sesame"
// When secret does equal sesame, exit the loop and write to the document
document.write("You know the secret password. Welcome.");