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

Max Gabriel
Max Gabriel
8,673 Points

I'm unsure what exactly goes on line 6?

.

script.js
var = secret
var = answer

do {
    prompt('What is the secret password?');
} while (answer === '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>

1 Answer

Andreas Nyström
Andreas Nyström
8,854 Points

Hi! A few things:

// Don't declare variables like this.
var = secret
var = answer
// Should be like this:
var secret = ''
var answer = ''

// do means do this at least one time. 
do {
// you should probably have the variable here, instead of outside the loop
    prompt('What is the secret password?');
// This says: keep doing the loop if the answer IS 'sesame'.
// What you want is to say: keep doing the loop if the answer IS NOT 'sesame'
} while (answer === 'sesame')  
document.write('You know the secret password. Welcome.');

So in short it should be something like this:

// Run the loop atleast once (do)
do {
  // Make one variable inside the loop instead of outside that will have the value of whatever the user writes in the prompt.
  var secret = prompt('What is the secret password?');
// Keep doing the loop until the value of secret is 'sesame'
} while (secret !== 'sesame')  
document.write('You know the secret password. Welcome.');