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

Nathan Jordan
Nathan Jordan
2,294 Points

greeting var will not call properly

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

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Nathan;

Let's take a look at this challenge in which we are asked to:

Set the 'greeting' variable, on line 29, by using the 'genericGreet' function in the context of 'andrew' with the array of arguments, 'args1'.

The relevant code you posted is:

 var greeting = genericGreet;

      greeting.apply (andrew, args1);

The Challenge is asking us to set the greeting variable to something, and we have been given that syntax already. It is asking us to set that variable by using the genericGreet function, again that was already provided so we need to focus our attention to the line 29:

var greeting = genericGreet;

So, what does the rest of the challenge say and mean? In the context of andrew with the array of arguments, args1. To the genericGreet function we need to apply those values. Our code on line 29 then would need to look like:

//  Call and Apply Challenge - Code Snippet Only
//  Task: 1 of 1


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

Make sense?

Ken

Nathan Jordan
Nathan Jordan
2,294 Points

Thanks Ken! This is great, I was trying to copy the method that was used in the last video, this makes more sense in this situation

Richard Atanasov
Richard Atanasov
6,804 Points

Thank you so MUCH!! I have been searching the web for two days to try to figure this challenge out! I was making it WAY more complicated than it needed to be! Again, THANK YOU! Ken Alger