3

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
}
5
  • can you please explain you code properly and send html code with js Commented Dec 5, 2018 at 7:29
  • Feel free to post HTML code in the question so anyone can try on your code and diagnose the problem well instead guessing! Commented Dec 5, 2018 at 7:39
  • 1
    The variable inputList is undefined in the context: output.innerHTML += inputList; because it was defined somewhere else. Commented Dec 5, 2018 at 7:40
  • // global variables you barely ever need (or want) global variables. Certainly not in this code. Commented Dec 5, 2018 at 7:50
  • I did go back and got rid of the global variables as I saw no use for them. And I see where inputList is undefined. Still trying to get the hang of using this site and learning how to add the correct part of html with the js Commented Dec 5, 2018 at 7:55

1 Answer 1

2

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);
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why is the if statement needed within the while loop? Shouldn't it keep building the list and prompting for an input with just the while loop until quit is entered? Sorry I'm still learning.
It's required because in this code snippet input is filling inside the while loop, so it can't be assessed before it gets quit command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.