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 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)); }

printList(playList);

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.

print(listHTML);

( 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. :)