程序假设要求用户在列表中输入待办事项或输入“退出”以退出并退出循环。子功能要求输入并添加到列表中。 Main函数将列表从JS输出到HTML。
在我看来,它看起来似乎正确,但是在我仍在学习中时,不确定为什么即使输入“退出”,它也陷入了提示输入的无限循环中。任何帮助表示赞赏!
object_id
答案 0 :(得分:-3)
<html>
<head>
</head>
<body>
<ul id="outputPart1">
</ul>
<script>
var output;
var input;
function buildList(input) {
"use strict";
// declare variables
var unorderedList;
var inputList;
input = prompt("Enter a to-do item or \"quit\" to stop: ");
unorderedList = document.getElementById("toDo");
inputList += "<li>" + input + "</li>";
unorderedList.innerHTML = inputList;
}
function outputList() {
"use strict";
// PART 1: YOUR CODE STARTS AFTER THIS LINE
// declare constants
const QUIT_CODE = "quit";
// declare variables
var output,i=0;
//input = prompt("Enter a to-do item or \"quit\" to stop: ");
while (input !== QUIT_CODE) {
//buildList();
++i;
input = prompt("Enter a to-do item or \"quit\" to stop: ");
output = document.getElementById("outputPart1");
output.innerHTML += (i+". "+input+"<br/>");
if (input === QUIT_CODE) {
break;
}
}
// end of code
}
outputList();
</script>
</body>
</html>
此代码打印列表。希望能帮到你