Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a `do...while` loop

Nazeim Brame
Nazeim Brame
6,544 Points

I 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.

script.js
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")
index.html
<!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
Steven Parker
210,151 Points

Some 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
Nazeim Brame
6,544 Points

This 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
Steven Parker
210,151 Points

I've added a line-by-line commentary to my answer.