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 trial

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

Vasko Jocevski
PLUS
Vasko Jocevski
Courses Plus Student 1,040 Points

Can't solve this !!!!

This is the code we used in the last code challenge. After learning about do...while loops, don't you think this would work better in the do...while style? Re-write the code to use a do...while loop. Important: In each task of this code challenge, the code you write should be added to the code from the previous task. Restart Get Help Check Work script.js index.html

1 var secret = prompt("What is the secret password?"); 2 while ( secret !== "sesame" ) { 3 secret = prompt("What is the secret password?");
4 } 5 document.write("You know the secret password. Welcome.");

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.");
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

5 Answers

var secret; do { secret = prompt("What is the secret password?");
} while ( secret !== "sesame" ) document.write("You know the secret password. Welcome.");

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

This challenge really is a good use case to demonstrate a while loop.

It might help to break down the steps before worrying about the code:

  1. Prompt the visitor for a password.
  2. While the password is not correct (not sesame) continue prompting for a password (forever) until the correct password is entered.
  3. When the correct password is entered exit the while loop with a false condition.
  4. Give the visitor a message that they entered the correct password
  5. The end

Does that help?

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Aha - that make more sense if you look at what's duplicated in the while loop (not DRY) could be simplified with a do while. MDN while vs MDN do while

The important thing about a do while is that the code in the do block is always executed at least once before checking the while condition.

So, you would combine steps 1 and 2 into the do block - does that make sense?

Abraham Juliot
Abraham Juliot
47,353 Points

EDIT: No, the do while loop will not "work better". But, it can accomplish the same thing as the while loop -- both work just fine.

none of you make any sense FYI. Nobody answered the question

Abraham Juliot
Abraham Juliot
47,353 Points

Sorry... I added an edit. Hope that helps for clarity :)