JavaScript

German Martinez
German Martinez
1,833 Points

Do while loop error!

I'm trying to complete a challenge but I can't seem to figure it out :(

Basically is asking me to convert a while loop in to a do while loop that will ask the user to enter password every time that is not equal to "sesame".

Here is my code:

var secret = prompt("What is the secret password?"); do { secret++; secret = prompt("What is the secret password?"); }while (secret !== "sesame");

document.write("You know the secret password. Welcome.");

4 Answers

Andrey Misikhin
Andrey Misikhin
14,842 Points

I give you an idea of how it may be. This code works for me, you may declare "secret" outside of while block.

var secret;
do { 
  secret = prompt("What is the secret password?");
} while (secret !== "sesame");

document.write("You know the secret password. Welcome.");
Andrey Misikhin
Andrey Misikhin
14,842 Points

Next time use markdown to format your code on the page.

do { 
var secret = prompt("What is the secret password?");
} while (secret !== "sesame");

document.write("You know the secret password. Welcome.");
German Martinez
German Martinez
1,833 Points

That did not work because that is converting a var secret every time the loop runs. My code works when I try in Chrome console but probably not the most efficient way to do it, the error I get says that I only need to call prompt() once.