JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a `do...while` loop

Gareth Partridge
Gareth Partridge
8,773 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

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 48,707 Points

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
8,773 Points

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

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 48,707 Points

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
8,773 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