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 trialRyan Schmelter
9,710 PointsIn 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
10,026 Pointsvar 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);
edinjusupovic
5,664 PointsCould you post the code please.
Alexander Sakaev
Courses Plus Student 1,757 PointsAlexander Sakaev
Courses Plus Student 1,757 PointsIt is basically can be any name in the parentheses.
function printList ( xxx ) { .....}
Clay Bowser
6,619 PointsClay Bowser
6,619 PointsWow that's so cool, thanks for the explanation!