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
khcir .
Courses Plus Student 8,527 PointsJavaScript Foundations > Objects > Call and Apply > Challenge 1/1
http://teamtreehouse.com/library/call-and-apply
The question say's that i have to set the variable on line 19, but there is no logic for that, instead it looks like i have to set the variable on the line 29. Is there a typo in the challenge question, or I don't understand it wright? . .- i'm really stack on this one, so any help, will be highly appreciated...
/here is my code so far...
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;
andrew.greeting.apply(andrew. args1);
9 Answers
khcir .
Courses Plus Student 8,527 PointsThe question on this challenge is very confusing, so let me put it that way:
"Change the "greeting" variable (line 29), by using the "genericGreet" (line 19) using "andrew" as a context, and the array of arguments "args1"
Here i will copy/paste the answer, but try first to understand and solve the challenge, rather than copy/paste the answer just to pass it...
var greeting = genericGreet.apply(andrew, args1);
Let me know if that make sense...
Jamil Smith
7,620 PointsThanks khcir. I really don't understand it but I gotta just move on. This stuff is putting me to sleep haha
Oliver Monk
1,867 PointsThanks for the reply, could you possible explain how it is that the second value in the args1 array is applied to the correct value that we want to pass into the function? I would have thought it would you would need to define the correct index? e.g. args1[1]
Xander Taylor
12,390 PointsI have a feeling this is one of those things that will make more sense once we run into a situation where we have to use it. We need context.
Brian CI
28,971 PointsI agree, terribly worded question.
Charles Steinmetz
14,420 Pointsvar greeting = genericGreet.apply(andrew, args1);
Shirley Lim
11,998 PointsMe too. I feel like I followed the instructions based on the video (think it's within the last two minutes) and I've arrived at a very similar answer to yours (except a comma in place of the period of the second andrew).
For now I'll move on, feel like it'll be more productive than trying to get the code to work.
khcir .
Courses Plus Student 8,527 Pointsyou are right about the comma part...
Shirley Lim
11,998 PointsOn second thought, I finally got it to greenlight! The instructions are confusing,and they do mean line 29. You have [most] of the right parts, just move it a line up.
They want you to 'set the greeting variable on line X' (hint: your answer should be on the same line). Hope that helps!
khcir .
Courses Plus Student 8,527 Pointsthank you for the hint: i finally got it, - this challenge is a real challenge, i mean - a hard one...
Shirley Lim
11,998 PointsNo problem, I was easily wracking my brain for two hours. It's easy to miss if you're not reading it literally. Glad you were able to figure it out.
Justin Harrison
Courses Plus Student 810 PointsThanks for the advice.
My code looks like this with the apply call on line 29 but still no go.
<script>
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;
andrew.greeting.apply(andrew, args1);
</script>
Am I missing something?
Thanks!
khcir .
Courses Plus Student 8,527 PointsYou have the right parts, just move it a line up.
They want you to 'set the greeting variable on line X' (hint: your answer should be on the same line). Let me know if you get it, if not i will copy/paste you the answer...
Bob Smith
6,711 Pointsvar 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;
andrew.greeting.apply(args1, andrew);
Still no pass? Not quite sure what's wrong with it... It's on line 29 not 19...
khcir .
Courses Plus Student 8,527 PointsThe question on this challenge is very confusing, so let me put it that way:
"Change the "greeting" variable (line 29), by using the "genericGreet" (line 19) using "andrew" as a context, and the array of arguments "args1"
Here i will copy/paste the answer, but try first to understand and solve the challenge, rather than copy/paste the answer just to pass it...
var greeting = genericGreet.apply(andrew, args1); Let me know if that make sense...
Justin Harrison
Courses Plus Student 810 PointsThanks!
Corey Baker
4,890 PointsGood explanation. I was getting frustrated because I wrote the code in Sublime Text and it was returning the proper result in my web browser in the Chrome Dev Tools.
Initial 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);
//in the console if you typed console.log(greeting.apply(andrew,args1));
//then you would get the response, "Hello Michael, my name is Andrew and I am in a awesome mood!"
//which is the correct answer
But they were looking for this:
var greeting = genericGreet.apply(andrew,args1);
It can be confusiong the way they word the questions....
Bob Smith
6,711 PointsThought I had said Thanks! Thank you khcir!
Gerardo Rodriguez
8,708 PointsMy code go well by puting this way, hope still help you...
var genericGreet = function(name, mood, face) {
name = name || "you";
mood = mood || "good";
face = face || "face";
return "Hello " + name + ", my name is " + this.name +
" and I am in a " + mood + " mood!" + face;
}
var andrew = {
name: "Andrew"
}
var args1 = ["Michael", "awesome", ":)"];
var greeting = genericGreet.apply(andrew, args1);
console.log(greeting);
Edwin Eames
5,886 PointsI was using ' ' ' ' :/
Edwin Eames
5,886 PointsI was using ' ' ' ' :/
Justin Harrison
Courses Plus Student 810 PointsJustin Harrison
Courses Plus Student 810 PointsGood question. I have the same issue.