简体   繁体   English

while循环使用html列表

[英]while loop with html list

Program should keep looping to ask user for input on a to do list until they enter "quit" to exit. 程序应该保持循环以询问用户输入待办事项列表,直到他们输入“退出”退出。 It works but only once as it does not loop as it should be. 它工作但只有一次,因为它不应该循环。 I need it to display the input as a list until "quit" is entered. 我需要它将输入显示为列表,直到输入“退出”。

Can't figure out why 无法弄清楚为什么

// global variables
var output;

function buildList(input) {
    "use strict";

    // declare variables
    var unorderedList;
    var inputList;

    unorderedList = document.getElementById("toDo");

    inputList = "<li>" + input + "</li>";

    unorderedList.innerHTML = inputList;
}


function displayList() {
    "use strict";

    // PART 1: YOUR CODE STARTS AFTER THIS LINE
    // declare constants
    const QUIT_CODE = "quit";

    // declare variables
    var output;
    var input;

    while (input !== QUIT_CODE) {
        input = prompt("Enter a to-do item or \"quit\" to stop: ");
        output = document.getElementById("outputPart1");
        buildList(input);
        output.innerHTML += inputList;
        if (input === QUIT_CODE) {
        break;
        }
    }

    // end of code
}

I made it a little simpler, also it works: 我让它变得更简单一点,也有效:

function buildList(input) {
    "use strict";

    var inputList;

    inputList = "<li>" + input + "</li>";
    document.getElementById("toDo").innerHTML += inputList;
}


function displayList() {
    "use strict";

    const QUIT_CODE = "quit";

    var input;

    while (input !== QUIT_CODE) {
        input = prompt("Enter a to-do item or \"quit\" to stop: ");
        if(input !== QUIT_CODE)
            buildList(input);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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