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 Method

Does anybody understand this challenge:

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

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

      var andrew = {
        name: "Andrew"
      }

      var ryan = {
        name: "Ryan"
      }
Jimmy Hsu
Jimmy Hsu
6,511 Points

Do you have a link to the specific challenge?

10 Answers

Markus Ylisiurunen
Markus Ylisiurunen
15,034 Points

For me it is working just fine. I think you've missed something. Did you remember to add comma after name : "Name"? And now that I think about the semicolon thing, I believe it's not required for objects but can be used if programmer wants to.

It worked just fine with this code:

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

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

var ryan = {
    name: "Ryan",
    greet: genericGreet
};

I haven't begun learning javascript yet so forgive me if I botch this. To me it sounds like you have to create a new function:

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

You will then call that function in the andrew or ryan functions to display the message. Think of it as your generic 'Print' function that you use to display information. In this context your display a message of hi and the person's name. Again sorry if I reiterated what may look obvious.

Thanks for trying but that isn't quite it....not sure what they are looking for?

Markus Ylisiurunen
Markus Ylisiurunen
15,034 Points

Hi Douglas!

I believe that you should add a 'greet' method to both of the objects. And you should set a 'greet' method to be 'genericGreet' function.

So I believe the code should look something like this:

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

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

var ryan = {
    name: "Ryan",
    greet: genericGreet
};

Hopefully this helps at least a little bit :)

Again, thanks for trying but that didn't work either.....this is a tough one!

Jimmy, here is the link to the Challenge: http://teamtreehouse.com/library/methods-3

Michael Strobl
Michael Strobl
22,417 Points

Markus' solution is almost right. You mustn't forget the ; after the variable at the end.

Markus Ylisiurunen
Markus Ylisiurunen
15,034 Points

Ah, sorry for that little mistake. I just forgot semicolons after variables. Thank you Michael for notifying that! It was my bad.

I edited my last answer and now it should be right, thanks to Michael.

I tried the code with semicolons and it still is not correct....thanks for all the great idea's, i think the wording in the question is too vague.

Markus, that was it!!!! I must have forgotten something, what an ordeal that was - thanks for all your effort!

Markus Ylisiurunen
Markus Ylisiurunen
15,034 Points

No problem, mate! You're welcome :)