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!

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 Data Using Objects The Build an Object Challenge, Part 2

Kayla Larson
Kayla Larson
1,222 Points

Can anyone help better explain this piece of code?

I really don't understand how you know how to add this code, but it's been in the last few exercises and I want to be sure I understand it, and not just use it, before moving on. I know it allows you to add html to the JS without going to the actual html code (at least I think) but is it always going to be the same exact code every time? Is it just memorizing it?

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

PS sorry I don't know how to quote the code on here.

Thank you!

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Hi first, checkout the Markdown Cheatsheet at the bottom to see how to post code.

Your code is defining a function and the whole purpose of defining a function is to reuse it over and over.

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

There are many handy tools out there and one is CodePen.io - to work through HTML, CSS and Javascript ideas. I created a pen for you to check out and hopefully it will answer your questions - CodePen Example

Go a head and check it out, mess it up (best way to learn is to break it and work out how to fix it). Then, if you still have questions, just ask.