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

I am unable to figure this one out. Any help would be appreciated.

I can't figure out what I'm doing wrong with my do while loop

var secret = prompt("What is the secret password?");
do ( secret !== "sesame" ) {
  secret = prompt("What is the secret password?");    
} while ( secret = "sesame" );
document.write("You know the secret password. Welcome.");
<!DOCTYPE html>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
<script src="script.js"></script>

1 Answer

The first condition you used is exactly what you need. You just need to rearrange it a little bit:

We don't know what the user will type in. So you could just leave that question open by assigning secret an empty string:

var secret = "";

The code after do gets executed at least once without checking any conditions. So you can use that opportunity to prompt a question and assign the answer to secret. AFTER the user types in an answer, the answer will be checked against the condition following while:

do {secret = prompt("What is the secret password?")}
while ( secret !== "sesame" );

If the user does not type in 'sesame' the condition is met and the loop will keep looping. Once he gets it right and the condition (which assumes he gets it wrong) is not met anymore the rest of the code gets executed:

document.write("You know the secret password. Welcome.");

Good luck!