Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Foundations Objects Methods

Michael Criste
Michael Criste
5,045 Points

Please see code. I can't see what's wrong.

I tried taking the console.log out of the function, in case they didn't want it, but I still couldn't pass.

index.html
<!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",
        greet: function genericGreet() {
          console.log("Hello, I am " + this.name);
          }
      };

      var ryan = {
        name: "Ryan"
        greet: function genericGreet() {
          console.log("Hello, I am " + this.name);
          }
      };

    </script>
  </body>
</html>

3 Answers

Hugo Paz
Hugo Paz
15,622 Points

Hi Michael,

genericGreet has already be defined, you just need to assign it to the greet property like so:

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

if you want to call it, you do:

andrew.greet()
Jesus Mendoza
Jesus Mendoza
23,275 Points

I don't know what are you trying to do but this prints both names in the console

<script>
      function genericGreet(name) {
         console.log("Hello, my name is " + name);
      }

      var andrew = {
        name: "Andrew"
      }

      var ryan = {
        name: "Ryan"
      }

      genericGreet(andrew.name)
      genericGreet(ryan.name)
</script>
James Alker
James Alker
8,554 Points

You don't need to recreate the genericGreet function in each object. The following should be fine:

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

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

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