Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Using For Loops with Arrays

listHtml += '</ol>';

function printList(arrayList) {
      var listHtml = '<ol>';
      for(i = 0; i < arrayList.length; i++) {
          listHTML += '<li>' + arrayList[i] + '</li>';
listHtml += '</ol>';
print (listHtml)

The part that I don't understand is when you do this below in side the printList function above.

listHTML += '</ol>';

If you do listHTML += '</ol>'

wouldn't that be '<ol>' += '</ol>' ??? I feel like listHTML += '</ol>'; is adding an extra '<ol> since listHTML is already being called after the for loop here... listHTML += '<li>' + arrayList[i] + '</li>';

So, I am not understanding that part. I know we are just trying to add the closing </ol> tag, but I don't undestand how its doing that in listHTML += '</ol>'; piece of code.

Also, what does += mean in that same piece of code? What am I not understanding?


1 Answer

Assuming there were items in the array listHTML might look like this:

listHTML = '<ol>'
listHTML = listHTML +  '<li>Coffee</li>' = '<ol><li>Coffee</li>'
listHTML = listHTML +  '<li>Tea</li>' =  '<ol><li>Coffee</li><li>Tea</li>'
listHTML = listHTML +  '<li>Milk</li>' = '<ol><li>Coffee</li><li>Tea</li><li>Milk</li>'

The last assignment outside the loop like you said adds the closing tag

listHTML = listHTML +  '</ol>'  =  '<ol><li>Coffee</li><li>Tea</li><li>Milk</li></ol>'