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