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

Martin Bornman
PLUS
Martin Bornman
Courses Plus Student 12,662 Points

JavaScript Object

My code is not running in the browser.And is exactly the same as instructor's.

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; }

var message = '<p>Hello my name is ' + person.name + '</p>; print(message);

1 Answer

Erik McClintock
Erik McClintock
45,782 Points

Martin,

There are two possibilities that I see from glancing over your code here.

1) Make sure, of course, that you have HTML that includes a div with the ID of "output", since this is the element you're targeting and attempting to fill

2) In your var message declaration, you're reopening the string after your insertion of person.name. This is likely the culprit.

You have:

// notice after "person.name +" you have another single quote ( ' ), then your semi-colon. You're reopening a string with that character.
var message = 'Hello my name is ' + person.name + '; print(message);

Try:

// notice the extra single quote after "person.name" is removed. We close our string out with "person.name", then call our print function on the next line
var message = 'Hello my name is ' + person.name;
print(message);

Or, alternatively, if you want to follow the code from the video exactly, you would wrap that in paragraph tags:

// notice the <p> tags around the whole string, and that we make sure to close the string after the closing </p> tag
var message = '<p>Hello my name is ' + person.name + '</p>';
print(message);

Give that a shot and let me know if you're on your way or if there seems to be a different issue!

Erik