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

nicholas maddren
nicholas maddren
12,793 Points

Why do the functions contain arguments?

I'm a little bit confused here, I have learnt about functions however why is Dave using the following code:

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 (var i = 0; i < list.length; i += 1) {
    listHTML += '<li>' + list[i] + '</li>';
  }
  listHTML += '</ol>';
  print(listHTML);
}
printList(playList);

Why does the 'print' function have a property on it of 'message'?

Also why does the 'printList' function have a property on it also? I's really confusing me.

1 Answer

Matthew Rigdon
Matthew Rigdon
8,223 Points

There are two different answers to your questions.

1) Why does the 'print' function have a property on it of 'message'?

It is very common to have a parameter for a function. In this case, the "print" function takes in any parameter, which it will call "message." Some functions need to take a parameter that it will use inside the function. An example below:

function addFiveToNumber(number):
    newNumber = 5 + number;
    return newNumber;

This function needs to receive a parameter that a human must input. Otherwise you could not add 5 to a number that you don't know. So if you call the function addNumberToFive(3) then the function will return 8. Each time you see number, it will become a 3.

2) Also why does the 'printList' function have a property on it also? I's really confusing me.

There are two locations where printList is called, so I will reference both of them. The first instance is where the function is being defined. The function needs to take in a list, list, so that it can print it out. list is the list that the function will perform operations on.

The second time printList is called, it is using the playList as the the parameter. This means that printList will now start to run using the information in playList. Every time you see you see list in the printList function, it will be replaced with playList's information, since we put that list into the parameter for that function.