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
Febby Gunawan
14,075 PointsCode challenge: Call & Apply
Hi there, I'm stuck at this code challenge and I need some help from you guys. The challenge is "Set the 'greeting' variable, on line 29, by using the 'genericGreet' function in the context of 'andrew' with the array of arguments, 'args1'."
Here's my current code
var genericGreet = function(name, mood) {
name = name || "you";
mood = mood || "good";
return "Hello " + name + ", my name is " + this.name +
" and I am in a " + mood + " mood!";
}
var andrew = {
name: "Andrew"
}
var args1 = ["Michael", "awesome", ":)"];
var greeting = genericGreet;
greeting.apply(andrew,args1);
What did I do wrong. when I ran it on my own and I get ""Hello Michael, my name is Andrew and I am in a awesome mood!"
Thanks in advance.
4 Answers
Andrew Shook
31,709 PointsThe problem is that you set the greetings variable equal to the function genericGreet() not the return value of the function. If you console log greeting from the code you have above, you should see that
greeting = function(name, mood) {
name = name || "you";
mood = mood || "good";
return "Hello " + name + ", my name is " + this.name +
" and I am in a " + mood + " mood!";
}
what you need to do is either:
var greeting = genericGreet.apply(andrew, arg1);
or
var greeting = genericGreet;
greeting = greeting.apply(andrew, arg1)
Febby Gunawan
14,075 PointsHi Andrew Shook , Thanks for the help. :) I wasn't sure what I do wrong but after I do the console.log it became clear for me that the greeting variable wasn't set the value of the function.
Andrew Shook
31,709 PointsFebby Gunawan, glad I come help.
Mark Thomas
8,174 PointsQuestion: can someone explain the difference between call/apply in this instance?
scott duke
5,033 PointsWow, I really need to start using the console.log(). @ Mark Thomas, I think that in this instance, one method can hold an array and the other, cannot.
James Barnett
39,199 PointsJames Barnett
39,199 PointsAndrew Shook - Remember our goal here on the forum is give help not answers, next time try giving an explanation and/or a hint instead.
Need more explanation on the distinction check this out.
Andrew Shook
31,709 PointsAndrew Shook
31,709 PointsSorry, I wasn't sure how to explain it without showing an example.
James Barnett
39,199 PointsJames Barnett
39,199 PointsAndrew Shook - Answering questions well is a skill just like asking good questions.
In general ...