Home > Software engineering >  Why is my 'if' statement not creating the alert?
Why is my 'if' statement not creating the alert?

Time:02-23

let input = prompt('What would you like to do: (New, List, Delete, Quit)');

while (input === "New") {
    const newToDo = prompt("What would you like to 'ADD' to the list?");
} if (newToDo !== undefined) {
    alert(`${newToDo} added to the list`);
}

CodePudding user response:

I'm not going to explicitly write the code here as that would be a lost opportunity for learning. However, here are some starting points that would benefit you:

  1. You shouldn't be using const for a variable whose value is expected to change.
  2. As mentioned in the comment, your variable is out of scope meaning it's unrecognizable outside the while loop. In order to resolve that, declare the variable prior to the loop.

CodePudding user response:

let newToDo; while (input === "New") {newToDo = prompt("What would you like to 'ADD' to the list?");} if (newToDo !== undefined){alert(${newToDo} added to the list);}

  • Related