Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 866
M.P.T. Avatar asked Sep 25 '26 02:09

M.P.T.


1 Answers

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