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 Two-Dimensional Arrays

function print(message) ?

I don't understand why do we need a function print(message) here. Why do we use 'message' as a parameter and what will this function do because we also have function printSongs below

Julia Ji
Julia Ji
13,266 Points

Hi Minh,

I really like the instructor but I found that part confusing too. I just coded it this way. It's a lot easier to understand.

var playList = [ ['I Did It My Way', ' Frank Sinatra'], ['Respect', ' Aretha Franklin'], ['Imagine', ' John Lennon'], ['Born to Run', ' Bruce Springsteen'], ['Louie Louie', ' The Kinsmen'], ['Maybellene', ' Chuck Berry'] ];

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

// I just removed the entire print method and replaced it with this.
document.write((listHTML)); }


2 Answers

You don't need a print function here, but it provides reusable, flexible code for the future.

The message parameter could be called anything, but message is suitable so we know to pass it the message/string we want it to work with.

In the video, Dave uses the printSongs function to construct a list of songs (hence the name of the function) as a string. Notice normally it wouldn't print or return anything.

function printSongs (songs){
  var listHTML = '<ol>';
  for(var i=0; i<songs.length; i+=1){
    listHTML += '<li>' + songs[i][0] + ' by ' + songs[i][1] + '</li>';
  listHTML += '</ol>';

Now, after constructing the list, he calls the print function to print out the listHTML string.


( function print) is a print function where you give a parameter and prints it. And (function printSongs) returns an unordered list of songs. But why you need the print function is something you have to watch the video for they will say why they use it. Because making useless functions is bad coding and they do not teach you that. Hope this helps. :)