Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,822 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.