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

Help, please.

I would really appreciate if someone helps me with this.

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>

2 Answers

To convert the code to a do while loop, there are a few changes that will be made

  1. remove the initial value set on the var secrets, this is because a do while loop is run once before the while condition is checked. If we did not remove it, then the prompt will be shown twice.

  2. follow the syntax of the do while loop, its kind of like a while loop upside down when you compare between them

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

Thanks!

Rahul Saini
Rahul Saini
4,611 Points

It asked you to convert while into do while. The main difference between them is that While : Runs at least once and then check the condition, whereas while checks the condition first and then runs the code.

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