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

Gareth Partridge
Gareth Partridge
13,421 Points

This is the code we used in the last code challenge. After learning about do...while loops,

I am getting a syntax error, not sure where it is though?

script.js
var secret = prompt("What is the secret password?");
do ( secret !== "sesame" ) {
  secret = prompt("What is the secret password?");    
}
while ( 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>

hey this is my answer, we only need to call the secret variable inside the loop not to create a variable inside the loop.

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

So I just call the secret variable inside the do while loop and this work. That takes a long

well that's work for me

3 Answers

You were closer the first time. From the link the syntax is:

do {
  code block to be executed
}
while (condition);

So you'd have:

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

The other hint was to declare secret as "" outside the loop.

hey this is my answer, we only need to call the secret variable inside the loop not to create a variable inside the loop.

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

So I just call the secret variable inside the do while loop and this work. That takes a long

Gareth Partridge
Gareth Partridge
13,421 Points

it is unfortunately still not working. I have tried to declare the secret outside the loop now in various ways.

Take a look at this page

Some hints

  • you have conditions to run in two locations
  • declare secret outside the loop as ""
Gareth Partridge
Gareth Partridge
13,421 Points

Hi Kris Thanks for replying.

I am still struggling with a syntax error, I have tried so many different methods now, I feel like I am stuck in a loop. this is my latest.

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

hey this is my answer, we only need to call the secret variable inside the loop not to create a variable inside the loop.

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

So I just call the secret variable inside the do while loop and this work. That takes a long