Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Confuse with (for loop) ...

function listitem(arr) {
    listHTML = "<ul>";
    for (var i = 0 ; i < arr.length ; i += 1) {
      listHTML += "<li>" + arr[i] + "</li>";
    };
    listHTML += "</ul>";
    return listHTML;
};

The correct answer :
1) time listHTML = "<ul>" + "<li>" + arr[0] + "</li>"
2) time listHTML = "<ul>" + "<li>" + arr[0] + "</li>" + "<li>" + arr[1] + "</li>"
3) time listHTML = "<ul>" + "<li>" + arr[0] + "</li>" + "<li>" + arr[1] + "</li>" + "<li>" + arr[2] + "</li>"

My logical :
1) time listHTML = "<ul>" + "<li>" + arr[0] + "</li>"
2) time listHTML = "<ul>" + "<li>" + arr[0] + "</li>" + "<ul>" + "<li>" + arr[1] + "</li>"
3) time listHTML = "<ul>" + "<li>" + arr[0] + "</li>" + "<ul>" + "<li>" + arr[1] + "</li>" + "<ul>" + "<li>" + arr[2] + "</li>"

If the for loop is a continuous function, why it not repeat the "ul" tag ?

1 Answer

Steven Parker
Steven Parker
244,190 Points

You only need one "<ul.>" tag because there is only one list. The loop creates the list items inside the list. After the loop finishes, there is also just one "</ul>" closing tag.