while循环与html列表

时间:2018-12-05 07:27:11

标签: javascript html loops

程序应不断循环询问用户在待办事项列表上的输入,直到他们输入“退出”退出为止。它可以工作,但只能循环一次,因为它不会像预期的那样循环。我需要它来将输入显示为列表,直到输入“退出”为止。

不知道为什么

// 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
}

1 个答案:

答案 0 :(得分:2)

我简化了一点,它也可以工作:

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