卡在其他if循环中

时间:2019-06-29 23:44:25

标签: javascript arrays if-statement foreach

我正在上课,我们正在创建一个待办事项清单。我的问题是,当我键入待办事项并按Enter时,会收到消息“ Enter new todo”,而不是“ Added todo”。看来我陷入了其他if循环,并且不会转到下一个if语句。

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

3 个答案:

答案 0 :(得分:2)

您需要将input的重新声明放在while循环内。还要检查input是否正确-这样,如果有人关闭提示框,它就不会崩溃。

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

答案 1 :(得分:1)

您的行:

input = prompt("What would you like to do?");在您的while循环之外,因此每次您输入待办事项时,input变量的值始终为“ new”。

查看所附的摘录以进行更正:

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

答案 2 :(得分:1)

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`