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

Not really understanding Methods. Help with this challenge please

The challenge question is: On 'andrew' and 'ryan', set the 'greet' method as the 'genericGreet' function.

<script>

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

  var andrew = {
    name: "Andrew"
  }

  var ryan = {
    name: "Ryan"
  }

</script>

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Gary;

Here is what the challenge says to do:

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

So, what do we need to do to accomplish this?

  1. Let's start with the general syntax for this challenge, the method name, Andrew or Ryan needs to have a comma after it and before the greet method. No semicolon is needed after the last method.

  2. The genericGreet function has the keyword this inside the function, so we don't need to use the name while calling the function as this will provide the necessary scope.

  3. genericGreet is a function, so we need to set the greet method to genericGreet, as asked in the challenge directions, this should allow ryan.greet() or andrew.greet() to call the genericGreet function with the associated string. So in each of the named variables (andrew & ryan) we need to specify a property and value as greet: genericGreet.

Are you with me thus far?

The script code would then be:

// COURSE: JavaScript Foundations
// Module: Objects
// Challenge: Methods
// Task: 1 of 1


    <script>


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

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

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

    </script>

I hope that helped, keep up the great work and happy coding,

Ken

Man your explanation was the bomb man! Thank you very much

Kyle Daugherty
Kyle Daugherty
16,441 Points

andrew and ryan are objects. Properties on objects can be functions. So, on each one, set a property named greet equal to the genericGreet function. Afterwards, if you were to call andrew.greet(), for example, it would return "Hello, my name is Andrew"

I don't get it. 'andrew' and 'ryan' are the objects for what I understand. Now the property you say I should name it 'greet' on both of the objects, but it would have to equal genericGreet function. So I understand it to look like greet: genericGreet. I know I'm off somewhere just not sure