Implement map on a Prototype Can anyone explain this to me?

Tell us what’s happening:
I really don’t understand this code. Why we put callback before (item)
This whole code is just too complex to me.
Can anyone please explain it to me?

Your code so far


// the global Array
var s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback){
  var newArray = [];
  // Add your code below this line
  this.forEach(function(item) {
      newArray.push(callback(item));
  }
  );
  // Add your code above this line
  return newArray;

};

var new_s = s.myMap(function(item){
  return item * 2;
});

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype/

It’s ok. Things are complicated at the beginning. And my explanation maybe too, but for lack of better expressive powers :slight_smile:

callback here is the parameter of the function in function(callback). When you then call myMap on s: s.myMap(function(item){...}) you pass a function, than takes the place of the parameter callback.
Then in the push part the code calls the function callback with item as its argument.

I still don’t understand what is callback. I mean yeah, I know that is a parameter in the function but my main question is why we write .push(callback(item));

What callback is supposed to do in this, I mean callback is not a function so why we don’t put “,” between it and (item)

Sorry for many questions but I’m really confused.

I think I understood that but why we write .push(callback(item)) instead of direct .push(item)?

Precisely, callback is a function, you can pass functions as arguments in javascript. With callback(item) you are using the fuction passed as argument, whatever it were, and giving item as its argument.