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

james rochabrun
PLUS
james rochabrun
Courses Plus Student 22,726 Points

why is he using in the for loop , list.length and list[i] if the name of the array is playlist?

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

why is he using in the for loop , list.length and list[i] if the name of the array is playlist? i am using playlist in the for loop and works fine, somebody can explain it to me please?

3 Answers

Cecilia Khiew
Cecilia Khiew
12,431 Points

I think you are trying to ask why is the 'list' parameter used in the function, as well as inside the for loop and listHTML, instead of the name of the array which is 'playList'.

Example: list.length, list[i]

Instead of : playList.length, playList[i] which you used inside your code.

If you remember about functions, it is something that you use inside your programs to so that you can repeatedly use them. Quoting from MDN, "Values can be passed to a function, and the function can return a value." In this case, let's say that we have another playlist that we want to display using this printList function. Let the name of the other playlist be playList2. So, if I were to call the function using printList(playList2); The printList function of yours will not work because the parameters you declared inside the function.. playList.length and playList[i] does not correspond to the parameter you declared as 'list' inside of printList(list).

The main use of 'list' is a parameter of the function printList. 'list' acts a label to 'contain' the items to be passed through the function.. which is in this case.. playList, playList2 or any other playList you declare. Therefore, in order for your function to work properly, you need to use list.length and list[i] inside the function.

In case if you are wondering why you code works. It's because you used playList.length and playList[i] inside the function which is pure coincidence.. as you are using the array named playList. Try experimenting it with playList2 to help deepen your understanding.

Happy coding~

highlandcow
highlandcow
7,352 Points

I had this exact question. Your explanation and suggestion to tinker with playList2 helped immensely! Thank you!

Ionut Ghita
Ionut Ghita
1,951 Points

I was also puzzled about that "list" parameter. Thank you Cecilia.

Robert Manolis
STAFF
Robert Manolis
Treehouse Guest Teacher

The "i" commonly used in a "for" loop is just a place holder. I guess it's supposed to stand for index. But you could put anything in there. It's just a variable meant to hold a number. A "for" loop runs a piece of code a set number of times. The first time through, the variable "i" is equal to whatever you want, in the case of your loop, it starts out at zero. Then the loop runs some code and adds 1 to "i". Now "i" is equal to 1 instead of 0, and the code in the loop will run again. And again and again for as many times as you declare.

The reason to use "list.length" is tell the loop to run once for every item in an array, so if you ever want to add something to that array, you won't have to change the loop iteration count as well.

Hope that helps.

Don't hesitate to ask a follow up question if needed.