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
Bradley Marsh
7,396 PointsClarification on one thing in this code.
I am a little confused with one thing from the video.
'list' is called into the function for printList, but I don't understand what list is referencing and how in this case. Why are we not passing playList and using playList.length for example?
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 (i = 0; i < list.length; i += 1) {
listHTML += '<li>' + list[i] + '</li>';
}
listHTML += '</ol>';
print(listHTML);
}
printList(playList);
1 Answer
andren
28,558 PointsYou are passing in playList, you just choose to name it list within the printList function.
When you define a parameter for a function you are essentially telling JavaScript "take the argument passed to the function and assign it to a variable equal to this name". Since playList is the first argument passed in to it and list is the first parameter list ends up containing the contents of playList. The names of arguments and parameters does not have to match, JavaScript does the data binding entirely based on the position of the argument/parameter.
You could rename the parameter to playList like this:
var playList = [
'I Did It My Way',
'Respect',
'Imagine',
'Born to Run',
'Louie Louie',
'Maybellene'
];
function print(message) {
document.write(message);
}
function printList (playList) {
var listHTML = '<ol>';
for (i = 0; i < playList.length; i += 1) {
listHTML += '<li>' + playList[i] + '</li>';
}
listHTML += '</ol>';
print(listHTML);
}
printList(playList);
And it would work fine, but that arguably makes the code more confusing, because now at a glance it looks like the printList function is acting directly on the playList variable defined outside the function. Which is incorrect as it is actually working with whatever list was passed in, which could be any list not just the playList.
The printList is designed to take any list you want to send it, while it's implementation is a bit overkill when you have just one list in your program you generally should always design your function to be pretty generic so that they can be reused later in the program for other similar tasks. Like printing a different list to the screen later in the program for example. If you wrote the function in such a way that it manipulated the printList directly then you could not use the function with any other lists.
Bradley Marsh
7,396 PointsBradley Marsh
7,396 PointsIt took me a couple of reads to comprehend it properly, but that has helped. Thank you very much.
andren
28,558 Pointsandren
28,558 PointsYeah it's pretty normal for beginners to struggle with function arguments/parameters when they first start with programming, but they are a fundamental part of using functions. Without them functions couldn't be written to be as reusable which is one of the main purposes of a function.
Once you have used and played around with functions a bit more you should start to get more of a hang on it. Practice makes perfect as they say.