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 Methods help: "On 'andrew' and 'ryan', set the 'greet' method as the 'genericGreet' function".

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

      var andrew = {
        name: "Andrew"
        Andrew.genericgreet();
      }

      var ryan = {
        name: "Ryan"
        Ryan.genericgreet();
      }
    </script>

11 Answers

you're almost there bud. Here's what it should look like:

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

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

    var ryan = {
        name: "Ryan",
        greet: genericGreet
    }
</script>

Alright, so what are we doing differently here? Well, first just some basic syntax errors right away. You'll notice that there needs to be a comma separating the methods name and greet and no semicolon is needed after the last method, in this case, greet.

So what else? Well, the parameter that you have set for the genericGreet function, person, is not needed. Why? Well, when we call either andrew or ryan's greet method, we use the this keyword inside the genericGreet function so that we know that we are calling the name method from within the correct scope.

The other key thing that you're missing here is how you're calling genericGreet. Right now you are calling genericGreet like a method, however it is a function. So what you need to do is set the greet method to genericGreet, as I did above, so that when you call either andrew.greet or ryan.greet it will call the genericGreet function and correctly display the correct string.

Hope I wasn't too confusing!

Ryan Bent
Ryan Bent
6,433 Points

Helped me as well!

Zachary Rolland
Zachary Rolland
16,465 Points

The video on methods is pretty complicated, but the challenge is more simple, in a way. That can be confusing sometimes. Thanks for the answers!

You're awesome Aaron Cochran . I've never touched JavaScript in my life pre-TeamTreehouse...so this is all very new to me. I'm absorbing it all slowly! Thanks buddy

Aaron Cochran so I just tried the code you gave me and it didn't work

You're welcome, and I would look carefully just to make sure you are typing it correctly. I just redid the challenge using the code I gave above and it did work. A lot of times with programming our errors turn out to be very simple ones such as a missed punctuation mark somewhere or an incorrectly spelled variable name.

anthony de alwis
anthony de alwis
4,146 Points

dont forget the comma after the name key

Nicholas Read
Nicholas Read
2,958 Points

Thanks Aaron Cochran, my only mistake was adding the parentheses calling the genericGreet function. So i had

greet : genericGreet()

I'm still a bit confused - I thought when we called a function, we needed the parentheses e.g.

jimGreet();

Why are the parentheses not needed when calling the genericGreet function within the Andrew / Ryan objects??

Nicholas Read
Nicholas Read
2,958 Points

I think I have a answer! genericGreeting is actually a variable that contains a function, not a function in itself, therefore we only need to call the variable, which doesn't need the parentheses. Sound right?

that is exactly right! Good job on digging in and finding the answer!

Nicholas Read
Nicholas Read
2,958 Points

I think I have a answer! genericGreeting is actually a variable that contains a fucntion, not a function in itself, therefore we only need to call the variable, which doesn't need the parentheses. Sound right?

Prabhakar Undurthi
Prabhakar Undurthi
4,846 Points

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

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

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

Brad Bucceri
Brad Bucceri
7,102 Points

My error was missing the commas separating the methods name and greet. Thanks for the explanation after your answer too.

Thanks guys, this helped me too.

Awesome Aaron I thank you for helping me to understand without just giving me the answer!!! Treehouse, this such a great idea to use, that'll help teach us! The whole here is the answer without giving the answer.

I'm not sure why, but it wouldn't work until I did this (adding the "this" to genericGreet):

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

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

    var ryan = {
        name: "Ryan",
        greet: this.genericGreet
    }
</script>