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 Accessing Object Properties

edmondo valvo
edmondo valvo
1,301 Points

Why does it only print the last thing i tell him to print?

First I print a full sentence and it works, but then when I change the age value and i print it, it only prints the age and not the sentence. Am I missing something?

This is the code: https://teamtreehouse.com/workspaces/28326182#

3 Answers

Adrien Contee
Adrien Contee
4,875 Points

When I clicked your workspace link it was broken but I'm going to attempt to answer based on what I think might be happening. (Shot in the dark -- I know)

Here's the print function for reference:

function print(message){
    var output = document.getElementById("someDiv");
    output.innerHTML = message;

Here's what I'm assuming you're doing:

print("This is a random sentence"); //First Argument
print("Lookey here, another one!"); //Overwriting Argument

//If you try to print it this way it wouldn't work because "output.innerHTML" is being ASSIGNED the message that is being sent to it as an argument in the print function. So the last print call ALWAYS overwrites the ones coming before it

Here's what it should look like:

var html = "";
html += "<p>This is a random sentence</p>";
html += "<p>Lookey here, another one!</p>";

//You declare a empty string variable and keep adding to it using the += self assignment operator (not sure if that's a real term, don't quote me) and then call the print function ONE time at the very end sending the variable holding all the text you want to print to the screen

Hope this helps. Good luck!

edmondo valvo
edmondo valvo
1,301 Points

Thanks Adrien, I understood what I was doing wrong. Anyway this is the code, and following your explanation I understood that the second print() was overwriting the other one. Thanks a lot!

var person = { name : 'Sarah', country : 'US', age : 35, treehouseStudent : true, skills : ['JavaScript', 'HTML', 'CSS'] };

function print(message) { var div = document.getElementById('output'); div.innerHTML = message; } person.name='Edmondo'; var message= 'Hello my name is '+ person.name +' I am from ' + person.country ; person.age +=1; print(message); print(person.age);

Rod Sanati
.a{fill-rule:evenodd;}techdegree seal-36
Rod Sanati
Full Stack JavaScript Techdegree Graduate 20,177 Points

I was noticing this in the final arrays challenge before this course.

It took me a good amount of time to work out what was happening, since that entire snippet of code where you are rewriting a div's innerHTML wasn't explained entirely, and we're told that we'll learn more about it in a later DOM course.

You can kind of see that you can only do it once too, since he keeps adding text to the single message variable