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
Kyle Brooks
6,753 PointsCode 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
Guled A.
10,605 PointsWhen 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();
Guled A.
10,605 PointsHello 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.
Kyle Brooks
6,753 PointsWatched 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?
Christopher Harding
4,201 PointsThat 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.
Kyle Brooks
6,753 PointsKyle Brooks
6,753 Pointsthat was one of the ones I tried, but was missing the comma between name and greet. Thanks for the help!!
Guled A.
10,605 PointsGuled A.
10,605 PointsNo problem. :)
Kyle Brooks
6,753 PointsKyle Brooks
6,753 Pointsnot gonna lie...JavaScript is my weakness. I much prefer CSS and HTML lol
Guled A.
10,605 PointsGuled A.
10,605 PointsDo 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.
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.
Kyle Brooks
6,753 PointsKyle Brooks
6,753 Pointsthat's awesome!! Never thought of doing that.
Travis Stewart
15,188 PointsTravis Stewart
15,188 PointsThanks, I was trying to initialize it.