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

Code Challenge: JavaScript Foundations - Objects - Challenge 1 of 1

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

URL: http://teamtreehouse.com/library/methods-3

Here's the script that you start with.

  <script>


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

      var andrew = {
        name: "Andrew"
      }

      var ryan = {
        name: "Ryan"
      }

    </script>

I've tried andrew.greet and ryan.greet , but can't seem to get the right answer.

4 Answers

When you use a period and then the property, you are actually initiating the property of the object without creating it first. Below is the actual code. What you needed was to create the greet property.

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

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

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

The question never asked to initiate or call on the greet function of the object. Just to set them. Hope this helped.

If the question asked to initiate the greet property (which it did not in this case) then you would use this below:

ryan.greet();
andrew.greet();

that was one of the ones I tried, but was missing the comma between name and greet. Thanks for the help!!

No problem. :)

not gonna lie...JavaScript is my weakness. I much prefer CSS and HTML lol

Do not worry my friend. I, and I am sure others, have struggled with this language in the beginning. My advice is to always practice by making small programs such as a program that calculates velocity using the formula (distance * time), small things like this will help. Over time you will get better. Do not ever get discouraged my friend, if you put you mind to it, you will be successful.

Here are things I do a couple times a week. I'd think of a small program (say a math formula (mentioned earlier) and apply it in code. I'll give you an example of my experiments: I take a formula that I've learned thoughout my life time, it can be hard, it can be easy, either way, I just pick one, say the velocity formula.

Velocity = distance/time;

Then I apply this with that I'm learning in Javascript. So I make a condition. If someone wanted to repeatedly find the velocity of an object, how would I do this. Well, Functions repeat code over and over as long as they are called, so I will go with that.

function velocity(numberOne, numberTwo) //<---User enters two numbers
{
var distance = numberOne; //<--- I store their number into a variable called distance
var time = numberTwo;      // <--- I store the other in this variable called time
var velocity = distance/time; // <---Formula applied here

return velocity; // <--- I then return it back to me here
}

var question1 = prompt("Enter the distance traveled"); // <--- I prompt the user to give me information
var question2= prompt("Enter the time it took");

velocity(question1, question2); // <--- I plug in their response into the function and the function takes 
//care of the process

Making small programs like this really help stimulate the mind into creating more programs, no matter how small or big. Soon enough you'll invent generators, apps, and useful tools for all to use. So never give up my friend, keep going. It will get easier and easier as time goes on. Have a happy new year.

that's awesome!! Never thought of doing that.

Thanks, I was trying to initialize it.

Hello Kyle,

Remember, the question asks to set the greet function into the two objects, Andrew, and Ryan. For example: Andrew and Ryan both have a name property. The question asks to add a greet property into both objects, and the greet property should use the function genericGreet.

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

I do not want to give the answer away. I would like you to retry it. If you get stuck, comment again, and I, or anyone else on the forum would be glad to help you.

Watched the video again and tried this and a few other things...still a no go.

  <script>


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

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

      var ryan = {
        name: "Ryan"
        ryan.greet ("name")
      }

    </script>

Getting a "Null" error. Am I even on the right track?

That stupid comma, I've spent more times ripping my hair out over syntax errors. I'll have the right answer and run it but it's wrong. I for forgot the comma. I don't realize it so I switch everything around. Do this 5 times and I forget what I was even doing in the first place. I am learning what not to do which is helpful cause I'm learning how to resolve errors. It's still frustrating. "Walk'' before run, but this core stuff is so drab It'd be nice to have something that's gets the blood flowing between the dull stuff. Like demonstrating some cool stuff we'll be able to do once students grasp this stuff.