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 JavaScript Foundations Objects Methods

Nthulane Makgato
PLUS
Nthulane Makgato
Courses Plus Student 19,602 Points

Javascript Method Challenge

Good day everyone,

I am currently struggling with the following challenge on methods in JavaScript.

"On 'andrew', set the 'greet' method as the 'genericGreet' function."

This is initial code that I must work on:

var genericGreet = function() {
    return "Hello, my name is " + this.name;
}

var andrew = {
    name: "Andrew"
}

I have tried many things, here are one of my efforts:

var andrew = { name: "Andrew", greet: function() { return "Hello, my name is " + this.name; } }

(i'm not sure why this second bit of code doesn't look like easy to read code, i'm sorry for the inconvenience.) I have also watched the video again, please explain what's wrong as I don't understand.

Thanks in advance for your assistance.

2 Answers

Neil Salazar
Neil Salazar
5,014 Points

Hi Nthulane,

Hopefully I can explain this, I'm still trying to 'master' Javascript.

They want you to add the genericGreet function to a new property/method called greet, under the objects Andrew and Ryan. So you would need to create a new property and set it to the function genericGreet. See the example below.

var andrew = {
        name: "Andrew",
        greet: genericGreet
      }

      var ryan = {
        name: "Ryan",
        greet: genericGreet
      }
Nthulane Makgato
PLUS
Nthulane Makgato
Courses Plus Student 19,602 Points

Thanks Neil, I think I got it wrong because I hadn't placed a comma where it was supposed to be.