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?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.