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

Michael Pietrzak
Michael Pietrzak
5,121 Points

Can't make do while loop

While loop works fine and I have no idea how to make do while loop.

var secret = prompt("What is the secret password?");
while ( secret !== "sesame" ) {
  secret = prompt("What is the secret password?");    
document.write("You know the secret password. Welcome.");
<!DOCTYPE html>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
<script src="script.js"></script>

1 Answer

Steven Parker
Steven Parker
172,106 Points

Those two loop types are pretty similar, the main syntax difference is for the "do/while" you put the word "do" in front of the conditional block, and you put the "while" with the condition after the block.

The main functional difference is that in the "do/while" loop, the block runs before the condition is tested (so always at least one time).