简体   繁体   中英

Stuck in else if loop in Javascript

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);

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);

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);

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`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM