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

Jonatas Maia
Jonatas Maia
4,386 Points

Confused about print(listHTML); on the function

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

Hi my function is working well getting items from the array. But why do I need to use the p print(listHTML); inside the function?

I'm confused about that.

1 Answer

Michael Hulet
Michael Hulet
47,912 Points

(Disclaimer: I sort of remember the course you're probably on, so I'm typing this from memory, so it may not be exactly right, but I'll do my best)

The print function you made previously takes a string and appends it to the DOM. The printList function above takes an array and formats it nicely with HTML and puts in in a string that the print function can understand. In other words, your printList function is mostly just formatting, and the print function is what handles actually showing the data on the page. Thus, if you don't call print at the end of printList, the nicely-formatted code will never be rendered, and nothing will ever actually change