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

Code Challeng: Methods

I can't figure out from the video how to set the greet method as the genericGreet function. I am completely stumped. Code so far:

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

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

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

4 Answers

i cant figure out neither, but somehow i fixed it, so here is the code...

/comment

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

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

Thank you so much! However, if something could possibly explain to me how this works? I'm slightly confused.

from what I can tell, it substitutes the function name, genericGreet, in for the actual function you would need to put into greet. it does not take a dot notation, such as andrew.name, since it asked you for the function not the name. So, since the question it asks is for you to put the function in for greet. and that function was already written and given a name. All you have to do is put the name of the function into greet for both ryan and andrew and the code will do the rest.

Yeah, it's because the variable genericGreet has the function you want to run stored in itself. So, to call the function, we can just put the variable genericGreet as the 'value' in the key-value pair of greet: genericGreet.

After banging my head...

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

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

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

Thanks!

Can anyone tell me why the code doesn't work if I put ; after greet: genericGreet??

Thanks

Did you put commas at the end of the previous lines?