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

Luca Spezzano
Luca Spezzano
10,417 Points

The function print(message)?

what the function: print(message) does?and why we use that?

6 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

I'd need to see more of the code, but it sounds like it does just that. It prints the message which would be a string passed in as an argument to the message parameter.

So in this case the parameter is a variable which has been given a value which could be a message like "Hello! How are you? This is a message".

Remember a parameter is a placeholder variable passed into parentheses at function declaration and the argument is the value passed in at function call.

Luca Spezzano
Luca Spezzano
10,417 Points

ok thanks but he didn't declare the var message.So what happen with this parameter?

Chris Higgins
Chris Higgins
6,813 Points

Dave the instructor put this function in this code, and all it does in this situation is do the whole document.write thing. He's doing this so that there isent any DRY code. he can just pass a new message threw the function any time he want to put something else in the print function. It would be the same as just using the document.write in this situation.

Just keep going at it. The instructor revealed a compelling reason for why he wrote it that way near the end of the course. It had something to do with the fact that the document.write function is frowned upon. He then went on to introduce a better method for writing to the document:

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;

Thus, by factoring out the print function, all we had to do to update the change was to change the print function like so, above. All points in our document where we called the print function would then reflect this change. So it does indeed concur to DRY programming principles. I had assumed that since document.write was a simple function then it wouldn't be necessary to factor it out. It turns out that the more important reason for doing so is that if there is some new method of accomplish something it would then make updating our functions a lot easier.

Bruno Dias
Bruno Dias
10,554 Points

I'm still confused about the print(message) function.

function print(message) { document.write(message); }

The "message" is not declared anywhere as mentioned before.

Bruno and Luca, I'm a beginner and was wondering the same thing about the function print(message) strategy as I'm not sure it was explicitly explained in the web development track so far.

A quick Google search found this good explanation (http://ow.ly/N7wUY ).

Don't let the word "message" trip you up. You can put the word "bacon" in there and it still works! (And thinking about bacon is waaay better than trying to find the parenthesis you forgot) It's basically a parameter by the name of whatever you want to call it. It's not until later that you specify what array you want to run through the print function.

read that link that James provided, I was confused about it too but after reading that article it makes sense.

Aaron Coursolle
Aaron Coursolle
18,014 Points

Ironically, James provided a link to Dave's, yes the instructor of this course, book.

Bruno Dias
Bruno Dias
10,554 Points

Thank you James. I read the link you provided and it's a bit more clear now. I am also buying David's book. It sounds like a support material for his Javascript course.

All the print function does it that it prints whatever you pass to it as an argument. In this instance it writes to the html document using document.write. We could have just document.write(listHTML) instead of print(listHTML) and we would have the same result. Hence, I think he could have included it merely for semantic purposes to correspond accordingly with printList. For me at least, I find the argument that it was included to demonstrate DRY programming principles tenuous, as the function just included one line of code.

Maya Han
Maya Han
6,069 Points

I'm a js newbie and had the same confusion as the people mentioned above. But your answer cleared it. Thanks!

Hi Seab, I would like to see the solution you're proposing using the document.write(listHTML) instead of print(listHTML). I tried it, but nothing rendered. Thanks!