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 trialGary Christie
5,151 PointsNot really understanding Methods. Help with this challenge please
The challenge question is: On 'andrew' and 'ryan', set the 'greet' method as the 'genericGreet' function.
<script>
var genericGreet = function() {
return "Hello, my name is " + this.name;
}
var andrew = {
name: "Andrew"
}
var ryan = {
name: "Ryan"
}
</script>
3 Answers
Ken Alger
Treehouse TeacherGary;
Here is what the challenge says to do:
On 'andrew' and 'ryan', set the 'greet' method as the 'genericGreet' function.
So, what do we need to do to accomplish this?
Let's start with the general syntax for this challenge, the method name,
Andrew
orRyan
needs to have a comma after it and before thegreet
method. No semicolon is needed after the last method.The
genericGreet
function has the keywordthis
inside the function, so we don't need to use the name while calling the function asthis
will provide the necessary scope.genericGreet
is a function, so we need to set thegreet
method togenericGreet
, as asked in the challenge directions, this should allowryan.greet()
orandrew.greet()
to call thegenericGreet
function with the associated string. So in each of the named variables (andrew & ryan) we need to specify a property and value asgreet: genericGreet
.
Are you with me thus far?
The script code would then be:
// COURSE: JavaScript Foundations
// Module: Objects
// Challenge: Methods
// Task: 1 of 1
<script>
var genericGreet = function() {
return "Hello, my name is " + this.name;
}
var andrew = {
name: "Andrew",
greet: genericGreet
}
var ryan = {
name: "Ryan",
greet: genericGreet
}
</script>
I hope that helped, keep up the great work and happy coding,
Ken
Kyle Daugherty
16,441 Pointsandrew and ryan are objects. Properties on objects can be functions. So, on each one, set a property named greet equal to the genericGreet function. Afterwards, if you were to call andrew.greet(), for example, it would return "Hello, my name is Andrew"
Gary Christie
5,151 PointsI don't get it. 'andrew' and 'ryan' are the objects for what I understand. Now the property you say I should name it 'greet' on both of the objects, but it would have to equal genericGreet function. So I understand it to look like greet: genericGreet. I know I'm off somewhere just not sure
Gary Christie
5,151 PointsGary Christie
5,151 PointsMan your explanation was the bomb man! Thank you very much