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

Kim Dallas
Kim Dallas
11,461 Points

do I need to create a whole new var

do I need to create a new var with prompt for password?

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

1 Answer

Adam Pengh
Adam Pengh
28,797 Points

You only need to declare the variable before using it inside the do...while loop.

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

it doesn't work adam