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

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.");