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

JavaScript

JavaScript 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);

Good question. I have the same issue.

9 Answers

The 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...

Thanks khcir. I really don't understand it but I gotta just move on. This stuff is putting me to sleep haha

Thanks 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]

I 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.

I agree, terribly worded question.

var greeting = genericGreet.apply(andrew, args1);

Shirley Lim
Shirley Lim
11,998 Points

Me 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.

you are right about the comma part...

Shirley Lim
Shirley Lim
11,998 Points

On 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!

thank you for the hint: i finally got it, - this challenge is a real challenge, i mean - a hard one...

Shirley Lim
Shirley Lim
11,998 Points

No 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.

Thanks 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!

You 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...

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(args1, andrew); 

Still no pass? Not quite sure what's wrong with it... It's on line 29 not 19...

The 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...

Thanks!

Good 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....

Thought I had said Thanks! Thank you khcir!

My 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);

I was using ' ' ' ' :/

I was using ' ' ' ' :/