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

Code 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

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

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

Sorry, I wasn't sure how to explain it without showing an example.

Andrew Shook - Answering questions well is a skill just like asking good questions.

In general ...

  • figure out how you'd fix the bugs in their code
  • diff the code
  • give them a few hints about where to look for the bugs.
  • Go online and search out the relevant documentation and/or tutorials to fill in the missing knowledge if they are using a procedure/data structure incorrectly
  • Give them links to best/most relevant example you found

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

Febby Gunawan, glad I come help.

Question: can someone explain the difference between call/apply in this instance?

Wow, 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.