-1

Hi guys I can't get this problem and I believe the syntax is correct but I keep getting this error when I enter list option.

script.js:5 Uncaught ReferenceError: Cannot access 'input' before initialization at script.js:5:5

let input = prompt("What would you like to do ?!!!!!")
const todos = []
while (input.toLowerCase() !== "quit" && input.toLowerCase() !== "q") {

    if (input === 'list') {
        console.log('++++++++++')
    }

    let input = prompt("What would you like to do ?!!!!!");
}
console.log("OK!! Quit the app")
node_modules
  • 4,790
  • 6
  • 21
  • 37
wannabedev
  • 32
  • 8
  • You are trying to redefine `input` , `let input = prompt("What would you like to do ?!!!!!")` Wouldn't `input = prompt("What would you like to do ?!!!!!"` suffice? – JP. K. Dec 22 '22 at 08:14

3 Answers3

0

You should remove let on the 2nd input (in while)

let input means you want to declare input again, but you already declare it in the global scope

let input = prompt("What would you like to do ?!!!!!")
const todos = []
while (input.toLowerCase() !== "quit" && input.toLowerCase() !== "q") {

    if (input === 'list') {
        console.log('++++++++++')
    }

    input = prompt("What would you like to do ?!!!!!");
}
console.log("OK!! Quit the app")

Just an additional note to my answer that let is also scoped.

Scoping variables means the first input and second input declarations are not relative (because of your redundant let in the second input), and then it checks that you use prompt for the second input declaration (in the same scope of while), but it goes before let input (the second one), so that's why that error shows up.

Nick Vu
  • 14,512
  • 4
  • 21
  • 31
0

enter image description here

You are redefining the input in line 8 or something. Look at the pic, it didn't throw an error for me when I removed let.

I removed the prompt commands not to run into another error since I need to import a package like readline-sync.

let input = prompt("What would you like to do ?!!!!!")
const todos = []
while (input.toLowerCase() !== "quit" && input.toLowerCase() !== "q") {

    if (input === 'list') {
        console.log('++++++++++')
    }

    input = prompt("What would you like to do ?!!!!!");
}
console.log("OK!! Quit the app")

^^ this should work

JP. K.
  • 159
  • 1
  • 18
0

Look. You define input in the uppermost block. Then you define the same input in while block. So, inside while js will use your second definition. And yes, at if(input === ...) you try to access input before initialization. Try to read this MDN:let

Vasyl Moskalov
  • 4,242
  • 3
  • 20
  • 28