Reputation: 329
I'm doing an exercise for a class and we are creating a Todo list.
My issue is that when I type in my Todo and press enter, I get the message Enter new Todo
, instead of Added todo
.
It seems I'm stuck in this else if
loop and it won't go to the next else if
statement.
var todos = ["Buy New Turtle"];
window.setTimeout(function() {
var input = prompt("What would you like to do?");
while(input !== "quit") {
if(input === "list") {
console.log("**********");
todos.forEach(function(todo, i) {
console.log(i + ": " + todo);
})
console.log("**********")
}
else if(input === "new") {
var newTodo = prompt("Enter new todo");
todos.push(newTodo);
console.log("Added todo");
}
else if(input === "delete"){
var index = prompt("Enter index of todo to delete");
todos.splice(index, 1);
}
}
input = prompt("What would you like to do?");
console.log("OK, YOU QUIT THE APP");
}, 500);
Upvotes: 1
Views: 331
Reputation: 2523
Your line:
input = prompt("What would you like to do?");
is outside of your while
loop, so everytime you enter a todo, the input
variable always has the value of "new".
Look at the attached snippet for corrections:
var todos = ["Buy New Turtle"];
window.setTimeout(function() {
var input = prompt("What would you like to do?");
while(input !== "quit") {
if(input === "list") {
console.log("**********");
todos.forEach(function(todo, i) {
console.log(i + ": " + todo);
})
console.log("**********")
}
else if(input === "new") {
var newTodo = prompt("Enter new todo");
todos.push(newTodo);
console.log("Added todo");
}
else if(input === "delete"){
var index = prompt("Enter index of todo to delete");
todos.splice(index, 1);
}
// this line was moved into the while loop
input = prompt("What would you like to do?");
}
console.log("OK, YOU QUIT THE APP");
}, 500);
Upvotes: 1
Reputation: 329
var todos = ["Buy New Turtle"];
newTodo = [];
window.setTimeout(function() {
var input = prompt("What would you like to do?");
while(input !== "quit") {
if(input === "list") {
console.log("**********");
todos.forEach(function(todo, i) {
console.log(i + ": " + todo);
})
console.log("**********")
}
else if(input === "new") {
var newTodo = prompt("Enter new todo");
todos.push(newTodo);
console.log("Added todo");
}
else if(input === "delete"){
var index = prompt("Enter index of todo to delete");
todos.splice(index, 1);
}
input = prompt("What would you like to do?");
}
console.log("OK, YOU QUIT THE APP");
}, 500);`enter code here`
Upvotes: 1
Reputation: 44125
You need to place the redeclaration of input
inside the while
loop. Also check if input
is truthy - that way if someone closes the prompt box it doesn't crash.
var todos = ["Buy New Turtle"];
window.setTimeout(function() {
var input = prompt("What would you like to do?");
while (input !== "quit" && input) {
if (input === "list") {
console.log("**********");
todos.forEach(function(todo, i) {
console.log(i + ": " + todo);
})
console.log("**********")
} else if (input === "new") {
var newTodo = prompt("Enter new todo");
todos.push(newTodo);
console.log("Added todo");
} else if (input === "delete") {
var index = prompt("Enter index of todo to delete");
todos.splice(index, 1);
}
input = prompt("What would you like to do?");
}
console.log("OK, YOU QUIT THE APP");
}, 500);
Upvotes: 2