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 trialNazeim Brame
6,544 PointsI don't know what to do. I'm following the steps, and its not working.
I don't know how to convert a while into a do while. This wasn't covered in the course, so I'm unfamiliar with the format. I don't know how to make it work the same.
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.");
const secret = prompt("What is the secret password?");
do {
secret;
} while (secret !== "sesame")
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
2 Answers
Steven Parker
231,268 PointsSome hints:
- a "while" loop starts with the "while" keyword and a conditional expression, then has a code block
- a "do while" starts with the "do", then has a code block, and ends with the "while" keyword and expression
- a "while" block runs after the expression is tested, a "do while" block runs before the expression is tested
Perhaps that will help. If you're still stuck, you may want to review the do...while loops video, or look at some of the other forum questions about this same exercise.
UPDATE — here is the example code with comments added:
/* (original code)
var secret = prompt("What is the secret password?");
while ( secret !== "sesame" ) {
secret = prompt("What is the secret password?"); <-- re-use this line
}
document.write("You know the secret password. Welcome."); <-- re-use this line
*/
// any bits of the original code above not re-used should be removed
// no assignment needed here, and "const" can't be used for variables that will change
const secret = prompt("What is the secret password?");
// put the "var secret" declaration here (but no assignment needed yet)
do { // correct!
secret; // put the assignment from the original loop body here
} while (secret !== "sesame") // correct!
// move the "Welcome" message line here
Nazeim Brame
6,544 PointsThis doesn't help me at all Steven. I re watched the video 3 times. I need a solution so I can identify what I'm doing wrong. Thank you.
Steven Parker
231,268 PointsI've added a line-by-line commentary to my answer.