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

Noquisi Burgess
Noquisi Burgess
2,714 Points

can anyone show me how to recode this in the do while style because my code doesnt seem to be working. Thx

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.");

Above is the original problem and Below is the code I wrote to rephrase it into a do while loop which I dont understand the reason for not working

var secret; do { var secret = prompt("What is the secret password?"); if (secret === "sesame") document.write("You know the secret password. Welcome."); } while (secret !== "sesame")

1 Answer

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 48,792 Points

The error message you receive is you should declare secret before the loop which you have done. But you also declare it a second time inside the loop. Remove var inside the loop so it is just secret = prompt...

Noquisi Burgess
Noquisi Burgess
2,714 Points

thank you so much! duuh smh, in hindsight its so simple but thx for getting back to me so soon to help me finish the challenge and move on.