1

It tells me variable secret can not be found.

// Display the prompt dialogue while the value assigned to `secret` is not equal to "sesame"
do {
  let secret = prompt("What is the secret password?");
} while ( secret !== "sesame");
// This should run after the loop is done executing
alert("You know the secret password. Welcome!");
iAmOren
  • 2,760
  • 2
  • 11
  • 23
  • `let` has a strict scoping rule: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let – Greedo Jul 30 '20 at 15:13
  • ``do { var secret = prompt("What is the secret password?"); } while ( secret !== "sesame");`` – henok Jul 30 '20 at 15:15
  • Does this answer your question? [What's the difference between using "let" and "var"?](https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var) – Vishesh Mangla Jul 30 '20 at 15:46

2 Answers2

1

let has block scope. Use const or var.

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

EDIT: On suggestion of @James

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

will work too since now let's scope is increased by defining it out of do while statement's block

Vishesh Mangla
  • 664
  • 9
  • 20
  • Since you moved the declaration outside the block, you could also use `let` here, but `const` will not work since you are redefining the variable with the prompt response. – James Jul 30 '20 at 15:38
0

Like other people already commented: let has a strict scoping rule. Use var or const instead. Or use let outside the block, one level higher.

Amel
  • 653
  • 9
  • 15