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

Henry Powell
Henry Powell
3,413 Points

Trouble with do...while loop

It says: Bummer: The prompt() method should go INSIDE the do...while loop. I already tried putting it in the loop and it said: Bummer: Put the prompt() method outside the loop or you'll recreate the variable.

 var secret = prompt("What is the secret password?"); 
do { 

    if (parseInt(secret) === "sesame") {
      document.write("You know the secret password. Welcome.");

   } while ( secret !== "sesame" ) 
<!DOCTYPE html>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
<script src="script.js"></script>

2 Answers

Steven Parker
Steven Parker
202,109 Points

I think you're mixing up the messages. The second one would have been: "Bummer: You should declare the secret variable before the loop. Otherwise, you re-create that variable each time through the loop."

So the "prompt" method does go inside the loop, but the declaration of "secret" (not the assignment) is what should be outside.

Henry Powell
Henry Powell
3,413 Points

Can someone please give me the code for this.