我面临两个问题。此代码应该打印'to do'列表



第一个问题是我必须键入'q'或'quit'超过2次才能获得console.log("OK!您已退出在控制台中打印的应用程序。另一个是,当我输入"list"时,我看不到打印出来的待办事项列表,直到我退出,这意味着我输入了"q"或"quit"。请问有人能解决这些问题吗?

let input = prompt("What would you like to do?");
const toDo = ['Visit Resham', 'Check progress of Barth certificate'];
while (input !== 'quit' && input !== 'q') {
if (input === 'list') {
prompt("List has been printed! ")
console.log('******************')
for (let i = 0; i < toDo.length; i++) {
console.log(`${i} : ${toDo[i]}`);
}
console.log('*****************')
}
input = prompt("What would you like to do?")
}
console.log("OK! You have quitted the app ")

在if语句中提示用户,可以用警告代替…但是,如果您想保留它,只需将提示符放在if语句之外的else语句中。

let input = prompt("What would you like to do?");
const toDo = ['Visit Resham', 'Check progress of Barth certificate'];
while (input !== 'quit' && input !== 'q') {
if (input === 'list') {
//alert("List has been printed! ")
input = prompt("List has been printed! ")
console.log('******************')
for (let i = 0; i < toDo.length; i++) {
console.log(`${i} : ${toDo[i]}`);
}
console.log('*****************')
}
else
{
input = prompt("What would you like to do?")
}
}
console.log("OK! You have quitted the app ")

我刚刚编辑了你的代码,希望它现在可以工作了。

let input = prompt("What would you like to do?");
const toDo = ["Visit Resham", "Check progress of Barth certificate"];
while (input !== "quit" && input !== "q") {
if (input === "list") {
console.log("******************");
for (let i = 0; i < toDo.length; i++) {
console.log(`${i} : ${toDo[i]}`);
}
console.log("*****************");
alert("List has been printed! ");
}
input = prompt("What would you like to do?");
}
console.log("OK! You have quitted the app ");

相关内容

  • 没有找到相关文章

最新更新