JS Exercise

Output:

Solution:


    let arrayOfNodes = [
        "one",
        { name: "two", nodes: [{ name: "2.1", nodes: ["2.1a"] }, "two point two"] },
        "three"
    ];

    const generateHTML = (nodes) => {
        let html = "<ul style='list-style: disc;'>";

        for (const node of nodes) {
            if (typeof node === "string") {
            html += `<li>${node}</li>`;
            } else if (typeof node === "object" && "name" in node && "nodes" in node) {
            html += `<li>${node.name}${generateHTML(node.nodes)}</li>`;
            }
        }

        html += "</ul>";

        return html;
    };

    generateHTML(arrayOfNodes);

Tip: You can try updating the output by opening the console and run this function:

handleInput([
 // you can put custom arrayNodes
]);

// example:

handleInput(["item1", "item2",  { name: "item3", nodes: [{ name: "3.1", nodes: ["3.1a"] }, "three point two"] }])