Functional Programming - Implement map on a Prototype

why this code work i do not get it

Your code so far

Array.prototype.myMap = function(callback) {
  const newArray = [];
  // Only change code below this line


  // 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;
};

Your browser information:

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

Challenge: Functional Programming - Implement map on a Prototype

Link to the challenge:

Which part exactly is giving you troubles?

1 Like

this part in the code above:
callback(this[i], i, this)
it does not make sense to me

Okay, but why? This depends with what exactly the callback function can be called with.

If you look at the MDN docs for the map method you can see the callback is passed the element, index, and the array map was called on.

The callback args

this[i] -> element
i -> index
this -> original array
1 Like

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