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

Patricia Snyder
Patricia Snyder
5,563 Points

How does list.length relate to the playList array?

In the following code, how does list.length in the printList function relate to the playList array? In other words, how does the .length portion after list 'know' that the array list has 6 items?

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

2 Answers

Hi Patricia,

The reason that the function is able to know the length of the playList array is because you pass in that array when you call the function at the very bottom of your code. When you pass in an array to the function as an argument, the printList function uses the argument specified in its creation, the list argument, as a reference to the array you put as an argument when you called the function i.e. the playList array. So, whenever you are inside the function, you can use the argument list to refer to the playList array or any other array that is passed into the function. And when you get the length of list, you are getting the length of the array passed into the function: in this case, the playList array.

I hope that it makes more sense after reading that!

Patricia Snyder
Patricia Snyder
5,563 Points

In the immortal words of Homer Simpson, "D'oh!".

Thanks, Marcus, that makes a lot of sense.

I thought I was the only person in the world who used phrases in that way! :D I unashamedly used a Frozen reference earlier and told someone to "let it go...let it gooooo!" :D You're welcome, Patricia!

Patricia Snyder
Patricia Snyder
5,563 Points

Ha, I did worse, I played the song on my iPhone when a co-worker wouldn't move on regarding a petty annoyance. In fact, I downloaded the song for just that reason.

Thanks again!

You have bested me this round! haha That sounds awesome. And no problem, I won't be on here after this coming week, but you might see me on the forums answering questions for the next week. Good luck and happy coding, Patricia!

Had to reread it but you made sense of it for me. Thanks Marcus!