Functional Programming - Implement map on a Prototype

Hello there im having trouble with this problem.

i have found the solutuion via the forums but i still cant wrap my head around it

the problem :

the solution:

Array.prototype.myMap = function(callback) {
  const newArray = [];
  // Only change code below this line
  for (let i = 0; i < this.length; i++) {
    newArray.push(callback(this[i], i, this));
  // Only change code above this line
  return newArray;

so i’m going through it in my head as.
the function is called on some array

someArray.ourFunction ()

through this invocation
the context of this is being defined as someArray.


for(let i=0; i<someArray.length; i++){
newArray.push(callback (someArray[i],  i,  someArray)) ;
// the params for .map()

so here we are passing the params of map into some unknown function and pushing the result

it makes sense that we are passing the params into the unknown function as map runs every element into the param function

but how are we forcing the function to have these parameters if when we write the function that goes within .map we arent listing them?

also wouldnt
callback(someArray[i]) suffice to get this done?

The method is designed to make the existing element being iterated over, the index of the element being iterated over, and the array being iterated available to the callback function. The reason you need to include the i, and the this as the other to arguments, is that you have no idea what the callback function looks like that the use is implementing. If they need to reference the index of the element in their callback function and you only supplied the element to the callback function, their code would not work as intended.