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! While you're at it, check out some resources Treehouse students have shared here.

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 Object-Oriented JavaScript (2015) Introduction to Methods Understanding this

Christopher Denny
Christopher Denny
10,460 Points

Not properly functioning?

I've run my code through JSHint and JSLint and am coming up with no errors, I've ensured my function does strictly what I've been requested to do and is landing me the following error:

The fullName method should just include the console.log() statement adding the firstName, space and lastName together

My code is included for your review.

object.js
var contact = {
  firstName: "Andrew",
  lastName: "Chalkley",
  fullName: function() {console.log(this.firstName + " " + this.lastName);},
};

contact.fullName();

2 Answers

I think your formatting is throwing you off. After the "function (){ " do a line break and line things up again. You'll see that you only need a semicolon to end the console.log statement, and put the closing brace on the next line. You also have an extra comma hanging out.

var contact = {
  firstName: "Andrew",
  lastName: "Chalkley",
  fullName: function() {
    console.log(this.firstName + " " + this.lastName);
  }
};

It's good coding practice to always line things up in this way. (easier in a "real" code editor, notepad++ is free)

Christopher Denny
Christopher Denny
10,460 Points

Heya, Hayes, thanks a bunch for the reply. I went back and tried your suggested fixes and actually originally wrote it that way, those were added because JSHint recommended them, so I figured it was worth a shot. I'm still getting the same error after removing the extra semicolon and comma.

Here's my edited code

var contact = {
  firstName: "Andrew",
  lastName: "Chalkley",
  fullName: function() {
    console.log(this.firstName + " " + this.lastName);
  }
}

contact.fullName();

Hmmm, I'm not sure. I just took your edited code and pasted it into my window and it worked. To be clear, I was working on Task 1 of 2.

Christopher Denny
Christopher Denny
10,460 Points

Not sure why it wasn't working before, but it's working now. Thanks a bunch for your time and effort, Hayes!

Christopher Denny
Christopher Denny
10,460 Points

Weird! Yeah, I was on task 1 of 2, I'll attempt again for knowledge's sake