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 trialFabio Silva
5,097 PointsHow does the printList function know it should receive the playList array as an argument?
I still don't get it! What if there were more arrays? How the function would know which array to access?
3 Answers
Marcus Parsons
15,719 PointsHey Fabio,
You are sending off the playList
array into the printList() function. The printList() function uses whatever array you pass into it to build the list items. It uses the variable list
that you declared in the beginning of the function as a placeholder for the array you passed in. You could use any array you like to print a new list of items, as I will pass in my favorite colors along with the playList array:
var playList = [
'I Did It My Way',
'Respect',
'Imagine',
'Born to Run',
'Louie Louie',
'Maybellene'
];
//My new array with my favorite colors
var myFavoriteColors = [
'Green',
'Blue',
'Orange'
];
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);
//I just added a new call to printList and passed in my array I created
//that has my favorite colors
printList(myFavoriteColors);
Fabio Silva
5,097 PointsOhh, I see it now!
The printList() function is being called using the playList array as an argument!
Like this: printList (playList);
Thank you a gazillion!
Marcus Parsons
15,719 PointsYou are very welcome, Fabio! Happy Coding! :)
Brandon Ashcraft
10,259 PointsI was confused by this as well until I studied it a bit. Dave went over the standard placeholders 'i' and 'j' for 'for loops'. Are there similar "best practice"placeholders for functions (x,y,z for example)?