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

SeHyun Choi
SeHyun Choi
3,441 Points

Array Looping Question

Below is the code I learned from the video. What I am confused about is list[i].

list is the argument that we put into the function printList(). And I'm not quite sure how to even ask this. It kind of looks like alert(array[i]) but it is used in argument[i]? Can someone please explain list[i ] for me please?

CODE

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



function print(message) {
 var mer = document.write(message);
 return mer;
}



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

1 Answer

Antony .
Antony .
2,824 Points

You can call the list argument anything you want, it has no meaning yet. But when you called your printList function you inserted the playList variable, which contains your array of songs. Then JavaScript replaces the argument list with the variable playList. So to summarize it, list is a placeholder for a variable you will call inside your function.

So then to iterate through the songs in your array you have to use brackets with your variable

list[i]

list: your playList variable.

[i]: iterates through your arrays index.

SeHyun Choi
SeHyun Choi
3,441 Points

Thank you for the reply. I am confused because the 'list' is not array it like a parameter in function and we are using it like it is array 'list[i]'

Antony .
Antony .
2,824 Points

That's because "list" is an array. Why is list an array? because you made it become an array. How? you called your "printList" function with your array variable called "playList" inside it's parantheses. If you were to put like a string or variable that isn't an array then it would return an error.

printList(playList);

You replaced the argument "list" with your array called "playList".

So basically, this is what you wrote:

//This is the code YOU wrote
function printList( list ) {

    var listHTML = '<ol>';

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

        }

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

Then after you call your function with your array variable inside

printList(playList); //playList = array variable

this happens:

//This is the code that runs after you call your printList function.
function printList( playList) {

    var listHTML = '<ol>';

        for( var i = 0; i < playList.length; i += 1 ) { //list gets replaced with playList.
        listHTML += '<li>' + playList[i] + '</li>'; //list gets replaced with playList here too.

        }

    listHTML += '</ol>';
    print(listHTML);
}
SeHyun Choi
SeHyun Choi
3,441 Points

Thank you very much.