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

Dos T
Dos T
2,522 Points

Hi, I need a help!

I don't understand why we need a function " print(message) " and what we will write here , any text? But why we need it, how it will affect for our loop or Play List? And why in function printList, we added parametr list, and then when call a function printList we wrote in argument of printList "playList"? Why we didn't write from the begining in parametr -"playList", not "list" ?

1 Answer

The print() function is just a regular function that was created to make writing data to the document faster. So instead of using;

document.write('<h1>Hello World</h1>') 

We can use;

var foo = '<h1>Hello World</h1>';
function print(foo) {
    document.write(foo);
}; 

So now anytime we wish to use document.write, we can use print(message-here) instead.

The print function also takes your message print(foo) and it uses what was passed to it, in this case, foo, and passes that to, in this case, document.write; which would then output the message into the document using the document.write functionality.

Additionally, the printList function is being passed the list (the list being playList); the thing to remember is that what you're passing to the function doesn't have to be the identifier itself. See;

var foo = 'example data'; 

function print(message) { // the "message" is only what the function is using as a placeholder to later identify it inside the function 
    document.write(message); // see here we are now using message again, this is what we originally passed to the function
}; 

In the above example, we can pass anything to the function and it will replace the (message) in a sense and use it; the print(message) - message part is only a placeholder. You can pass print('Hello!') and it will work fine.