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 trialsebbe12
4,015 Pointsfunction print(message) { document.write(message); } I don't really understand
What does this function do exactly. It seems like it hold the texts/info from function printList but how.
Without it everything is gone. In every other exercise we have done in the course from the beginning til now only used document.write() to get something on the page why do we have to do a function now?
2 Answers
Jeremy Carbaugh
2,736 PointsIt's not uncommon to see people write functions (often called 'abstractions') that make it simpler to call other things.
So in this case I imagine they do this because 'print' is shorter than 'document.write()'. Also 'Window.print()' is a built in function in Javascript so they may building up a mental reference for the students to the print function. (https://developer.mozilla.org/en-US/docs/Web/API/Window/print)
Tomas Svojanovsky
26,680 PointsDocument is an object which holds some properties (f.e. document.images) and functions. In your example it is this - https://developer.mozilla.org/en-US/docs/Web/API/Document/write.
Function print take a parameter message and put it into document.write. It means - take the variable and write it into document. You can try it in the console of your web browser. It deletes all the content and puts it between body tag.