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

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Did I get this correctly (what's the role of 'songs' inside the function)?

I had this issue before, when creating the first example of displaying the playlist using one dimensional arrays. I have this piece of code

        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>';
   print(listHTML);
 }

 printSongs(playList);

Is this the same as writing the code like this?

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

  printSongs(playList);

If so, it means that the placeholder "songs" was used just for creating the function and it has no other role?

Did Dave changed the value "list" with the value "song" to be more adequate as a name or it has other implications? Thank you!

1 Answer

Tom Byers
Tom Byers
13,005 Points

Hi Arikaturika,

I'm not sure of the specific context here but I hope I can help.

The use of both songs and playList in the function are simply arguments that are passed into the function. They act as placeholders, as you say. They could be given pretty well any name you like. For example:

// create a function called printSongs:
function printSongs(anArray) {
    var listHTML = '<ol>';

    for (var i = 0; i < anArray.length; i += 1) {
        listHTML += "<li>" + anArray[i][0] + "by" + anArray[i][1] + "</li>";
    }

    listHTML += '</ol>';
    print(listHTML);
}

// call the function, passing in your actual array (e.g. songs, playList etc).:
printSongs(songsList);

I think you've got it. It's just a placeholder, ready for when you call the function with the actual variable name.