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 Adding a Method to an Object

Aaron HARPT
Aaron HARPT
19,845 Points

JavaScript code challenge

I am having trouble with the following code challenge:

Create a method on the contact object called fullName, use the programming from the printFullName function.

object.js
function printFullName() {
  var firstName = "Andrew";
  var lastName = "Chalkley";
  console.log(firstName + " " + lastName);
}

var contact = {
  fullName: "Andrew Chalkley"
}

4 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Aaron, What the challenge wants is for you to basically move the code from above INTO the 'contact' object. By doing so and using the name fullName... fullName now becomes the method for the contact object.

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

You are adding a method to a object. If it's still unclear, I'd recommend just re-watching the video right before the challenge, where Andrew explains it.

Keep Coding! :)

Thanks Jason, your advice helps me ;-)

You can get rid of the last variable

var contact = { fullName: "Andrew Chalkley" }

and then just run the function like so:

printFullName();

I believe that's the answer the task is looking for. Does this make sense, or would you like me to try and explain it in more detail?

Copy and paste the printFullName function into the contact object. Then make it an anonymous function by removing the function name "printFullName".

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

The questions make no sense, please rephrase them so they can actually make sense to someone trying to learn JS face palm -_-