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

Jonatan Luna
Jonatan Luna
2,554 Points

Whats wrong?

I´ve trying to do right, but I can do!

Help please

script.js
   var secret = prompt("What is the secret password?");

do {
  secret = promt("What is the secret password?");
  document.write("You know the secret password. Welcome.");

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

}
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

Christopher De Lette
PLUS
Christopher De Lette
Courses Plus Student 7,139 Points

Hi Jonatan Luna

The do..while loop is when you want a specified statement to run at least once before checking the condition. In this case you want the 'do' code block to run once then test the while condition. It would go as follows:

var secret;

do {
    secret = prompt("What is the secret password?");

   }while ( secret !== "sesame" );

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

The prompt of "What is the secret password?" will run once, allow the user to input an answer, if they do not get it right then the prompt is repeated X number of times until the condition == true. Then the print statement will be written to the screen.

Hope this helps

Take care and Happy Coding!