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 JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Using For Loops with Arrays

Sandra Vu
Sandra Vu
3,525 Points

Please explain the logic of the sequence of adding the HTML tags of <ol> and <li> in JS code

Please help me understand of the logic of adding HTML tags as in the code below:

var listHTML = '<ol>'; for (var i = 0; i<list.length; i+= 1) { listHTML += '<li>' + list[i]+'</li>'; } listHTML+= '</ol>';

Any recommendation for reading about adding HTML elements in JS code?

Many thanks

1 Answer

Steven Parker
Steven Parker
231,275 Points

:point_right: The tag <ol> begins an ordered list, and <li> begins a list item.

So this code starts an ordered list, creates a number of list items inside it with start and end tags for each one, and finally completes the ordered list with the end tag.

What's being done here is pretty straightforward HTML creation. If you're unfamiliar with the HTML code itself, you might enjoy the course on HTML.

Sandra Vu
Sandra Vu
3,525 Points

Thanks Steven. I shall look at the course on HTML