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

Ryan Schmelter
Ryan Schmelter
9,710 Points

In this video, does playList take the place of list when the function is called?

I'm confused about why he put -- list -- as the parameter of the printList function. Am I correct to say that in the code -- printList(playList); -- (playList) takes the place of (list) to run the array through the code. If so, why not simply use (playList) as the parameter instead of making a new one?

2 Answers

Annalyn Sarmiento
Annalyn Sarmiento
10,026 Points
var playList = [
  'I Did It My Way',
  'Respect',
  'Imagine',
  'Born to Run',
  'Louie Louie',
  'Maybellene'
];

function print(message) {
  document.write(message);
}

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

In this code, you're correct that list is the parameter of the printList function. When we call the function we pass the array stored in the variable playList into the function.

You could use playList as the parameter in the printList function, but using an arbitrary parameter, like list, allows the function to be reused for other arrays that may be created.

For example, say you create a new array called newList. You would simply have to call the function again, this time passing in the array newList :)

var playList = [
  'I Did It My Way',
  'Respect',
  'Imagine',
  'Born to Run',
  'Louie Louie',
  'Maybellene'
];

var newList = [
  'one',
  'two',
  'three',
  'four',
  'five',
  'six'
];

function print(message) {
  document.write(message);
}

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);
printList(newList);
Alexander Sakaev
Alexander Sakaev
Courses Plus Student 1,757 Points

It is basically can be any name in the parentheses.

function printList ( xxx ) { .....}

Wow that's so cool, thanks for the explanation!

Could you post the code please.