gpt4 book ai didi

javascript - 卡在 else if 循环中

转载 作者:行者123 更新时间:2023-11-30 09:13:32 25 4
gpt4 key购买 nike

我正在为类做练习,我们正在创建待办事项列表。

我的问题是,当我输入我的 Todo 并按回车键时,我收到消息 Enter new Todo,而不是 Added todo

看来我陷入了这个 else if 循环,它不会转到下一个 else 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);

最佳答案

您需要将 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);

关于javascript - 卡在 else if 循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56821534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com