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

objects

I cant figure out how to make andrew and ryan's greet function equal to genericGreet(), so I gave each object its own greet function. it is the right output but the assignment is aking me something else. Can you please show me how I can set each greet method equal to the genericGreetFunction(). thanks

<!DOCTYPE html> <html lang="en"> <head> <title> JavaScript Foundations: Objects</title> <style> html { background: #FAFAFA; font-family: sans-serif; } </style> </head> <body> <h1>JavaScript Foundations</h1> <h2>Objects: Methods</h2> <script> /* var genericGreet = function() { return "Hello, my name is " + this.name; }*/

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

  };

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

  };

  console.log(andrew.name);

</script>

</body> </html>

7 Answers

Adam Moore
Adam Moore
21,956 Points

Your answer above where you said, "I tried running the code using your advice but nothing prints to the console. Also, in greet: genericGreet is 'genericGreet' a variable storing a method?" should work, assuming you leave in all of the previous information. But just in case, here is the code that I did that just passed the challenge:

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

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

      var ryan = {
        name: "Ryan",
        greet: genericGreet
      }
Adam Moore
Adam Moore
21,956 Points

With the function defined above the objects, you can use greet: genericGreet to set the method greet to the function stored in the variable genericGreet.

Hello Adam,

I tried running the code using your advice but nothing prints to the console. Also, in greet: genericGreet is 'genericGreet' a variable storing a method?

Thank you!

<html lang="en"> <head> <title> JavaScript Foundations: Objects</title> <style> html { background: #FAFAFA; font-family: sans-serif; } </style> </head> <body> <h1>JavaScript Foundations</h1> <h2>Objects: Methods</h2> <script> var genericGreet = function() { return "Hello, my name is " + this.name; }

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

 };

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

  };

 andrew.greet();

</script>

</body> </html>

Adam Moore
Adam Moore
21,956 Points

It wouldn't print anything to the screen simply by setting greet to be genericGreet because you aren't telling code to run anything; only that the property greet belongs to both andrew and ryan. However, for the code challenge, you are just being asked to set the greeting method to each of andrew and ryan.

And yes, by declaring the function with var genericGreet = before it, you are assigning it to a variable named genericGreet.

Does this make sense? If not, let me know.

Hello Adam,

Do you know if these problems come with a final answer

thanks!

Adam Moore
Adam Moore
21,956 Points

What do you mean by a "final answer"?

oh, I mean the final answer that the system will recognize. Right now, I dont think I will get it on my own. thanks!

Hmm, not sure how this system works, I just typed you a thank you message, and it is no longer here. Anyway, thank you so much Adam for your help

Adam Moore
Adam Moore
21,956 Points

No problem! Glad I could help! :)