Tom Hsu
Tom Hsu

Reputation: 39

How do I replace the "var" keywords in this code with "let" and "const"?

If I replace the "var" keywords with either "let" or "const", I get a userChoice is not defined error.

I have already tried replacing everything with either "let" or "const". I have also put userChoice into a function and invoked the function whenever I needed it. I have also tried putting the entire while loop into a function. The programs works perfectly with "var".

The furthest I got with "let" and "const" is this / As soon as I tried putting it into a while loop it stopped working:

const arrayList = [];
let userChoice = prompt('What would you like to do?');
// Array list is empty array
if (userChoice === "new") {
    let newTodo = prompt('Enter a new todo');
    arrayList.push(newTodo);
} else if (userChoice === "list") {
    console.log(arrayList);
}

Working code:

var arrayList = [];
var userChoice = prompt('What would you like to do?');
// Array list is empty array
while (userChoice !== "quit") {
    if (userChoice === "new") {
        var newTodo = prompt('Enter a new todo');
        arrayList.push(newTodo);
    } else if (userChoice === "list") {
        console.log(arrayList);
    }
    var userChoice = prompt('What would you like to do?');
}

I expect this program to continually prompt unless you say "quit". You type "new" to add a new todo, and "list" to list all todos.

Upvotes: 1

Views: 235

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074555

The problem is that that code declares userChoice twice, the second var on it is unnecessary:

var arrayList = [];
var userChoice = prompt('What would you like to do?');
// Array list is empty array
while (userChoice !== "quit") {
    if (userChoice === "new") {
        var newTodo = prompt('Enter a new todo');
        arrayList.push(newTodo);
    } else if (userChoice === "list") {
        console.log(arrayList);
    }
    var userChoice = prompt('What would you like to do?');
//  ^^^---------------------------------------------------- here
}

With var it didn't matter because var has function scope (or global scope, if used outside a function) and multiple declarations are ignored, but it matters with let because let is block-scoped, so declaring a new userChoice within the block shadows the outer one. (Multiple declarations with let are an error in the same scope, since it doesn't make sense to do that, and allowing it would have complicated things.)

Just remove that second var and replace the other vars with let (or you could use const for arrayList and newTodo, unless code not shown assigns a new array to arrayList later):

const arrayList = []; // Or let
let userChoice = prompt('What would you like to do?');
// Array list is empty array
while (userChoice !== "quit") {
    if (userChoice === "new") {
        const newTodo = prompt('Enter a new todo'); // Or let if you prefer
        arrayList.push(newTodo);
    } else if (userChoice === "list") {
        console.log(arrayList);
    }
    userChoice = prompt('What would you like to do?');
}

Upvotes: 2

Related Questions