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

sakchai charoensin
PRO
sakchai charoensin
Pro Student 1,112 Points

I'm stuck at the "do...while" loop challenge.

Could someone tell me what is wrong with my code?

script.js
var secret ;
var correctPass = false ;
var passWord = 1234 ;
do{
  secret = prompt("What is the secret password?");
  if( parseInt(secret) === passWord ){
     correctPass = true ;
     }
}while( !correctPass )
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

Piotr Manczak
Piotr Manczak
Front End Web Development Techdegree Student 13,754 Points

Try this:

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

Leonardo Rubiano
Leonardo Rubiano
Full Stack JavaScript Techdegree Student 8,969 Points

Hey sakchai,

Your answer looks correct. I tried it in the console and it seems to be working as expected. Have you tried restarting the challenge or refreshing the browser? Make sure you're not changing the names of the original variables.

sakchai charoensin
sakchai charoensin
Pro Student 1,112 Points

I have done to start but it still won't work. Anyway, I try as the suggestion above from Piotr Manczak. It works now, thanks for your kindly attention.