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

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?

Thanks!

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>'