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

Azsha Preble
Azsha Preble
5,090 Points

Do... while loops are killing me

I greyed (noted?) out the original while loop. I'm supposed to turn it into a do... while... loop, but I can't figure out how. There are no hints, it just keeps coming back with a parse error. Can someone help me figure out where I'm going wrong?

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

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

} while (secret !== "sesame")
  if secret === "sesame"
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>

you dont need the if statement just document write.

Ezra Siton
Ezra Siton
12,644 Points
/* script.js */
var secret = prompt("What is the secret password?");
do {
  secret = prompt("What is the secret password?");
  if (secret === "sesame") {
    document.write("You know the secret password. Welcome.");
  }
} while (secret !== "sesame");

Related to you code your should put the if inside the do (Also you missing curly braces for ifstatement - syntax error) - Useful test your code validation her: https://esprima.org/demo/validate.html

In this code if the user write the correct password - he gets message "you know..."