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

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,289 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
      };