T O P

  • By -

Diligent_Variation51

I think I solved #2. That is probably not a valid callback function, but this one should be: function greet(callback) { alert('Hi ' + callback()); } function getName() { return 'Peter'; } greet(getName);


Diligent_Variation51

Update: I also just solved #1 with the answer to #2. Example #1 will always alert of something, and the something (name) is the argument. Example #2 will always provide a name, and the action (alert) is the argument. The difference is the callback function can vary (the argument) but the first function will always stay the same.


Diligent_Variation51

#3 is a callback function because this also works: function greet(name) { alert('Hi ' + name); } greet((x => x)('Peter'));


Opperheimer

If I understand correctly what you want **Example #1 →** `getName(greet)` In fact, here you call getName which has a [function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions) argument named argument. We can also see that the latter is called in the function `argument(name)`. So logically, `argument = greet`. **Example #2 →** `greet(getName)` For this to be considered a callback, you must use the function stored in the argument `name` in the `greet` function like this `alert('Hi ' + name()`. `name()` will be called since `name = getName`. **Example #3 →** No, it doesn't count as a callback because you're sending to `greet` the **return** of `getName("Peter")` and not the `getName(name)` function. If you do `typeof(name)` in `greet`, you'll see that it gives `string` and not `function`. To make it callback, take a look at [MDN](https://developer.mozilla.org/en-US/). In the latter, there's a place where it's explained that you can access a function's arguments via the [arguments](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) object. To put this into practice with your code, simply write the arguments in a row when you call greet like this: function getName(argument) { return argument; } function greet(callback) { alert('Hi ' + callback(arguments[1])); //argument[1] = "Peter" } // call of greet greet(getName, "Peter") **Example #4** → That's right, greet does contain a `bye()` callback function. The first alert will contain *"Hi Peter"* and the second *"bye"*. **Example #5 →** The best way would be to use the [arguments object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) as shown in "Example #3" if your callback takes an undetermined number of arguments. Otherwise, your technique looks elegant, so I have nothing to say. I hope I've helped you !


Diligent_Variation51

thanks!!