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 triallata sharma
iOS Development Techdegree Student 5,292 Pointsusing genericGreet function in context of andrew and passing array of args1
Set the 'greeting' variable, on line 29, by using the 'genericGreet' function in the context of 'andrew' with the array of arguments, 'args1'.
7 Answers
cpauciello
26,629 PointsYou need to use the apply()
method, as it will let you invoke the function with arguments as an array (or an array like-object).
See here
Pseudo syntax:
theFunction.apply(valueForThis, arrayOfArgs);
Answer:
var greeting = genericGreet.apply(andrew, args1);
cpauciello
26,629 PointsAh ok, use .apply(andrew, args1)
on the first genericGreet
, and remove that last line.
So instead of:
var greeting = genericGreet;
genericGreet.apply(andrew, args1);
It should just be:
var greeting = genericGreet.apply(andrew, args1);
lata sharma
iOS Development Techdegree Student 5,292 PointsChris,
I tried that already and this is the error msg I get: Bummer! The 'greeting' variable should be 'Hello Michael, my name is Andrew and I am in a awesome mood!'.
cpauciello
26,629 PointsWell that's weird, passes for me every time. can you post your 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.apply(andrew, args1);
lata sharma
iOS Development Techdegree Student 5,292 PointsHere it is:
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;
genericGreet.apply(andrew, args1);
lata sharma
iOS Development Techdegree Student 5,292 PointsOh wow! That did work! Not sure why it made the difference?
Thanks so much Chris!
cpauciello
26,629 PointsYou're welcome. :)