Functional Programming - Implement map on a Prototype

Tell us what’s happening:
Describe your issue in detail here.
Could someone please explain why the callback function has 3 parameters (this[I], I, this) initially my code just had 1 parameter for the callback function (this[I]) but I don’t understand why (I, this) are also parameters and what they do
Your code so far

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Safari/605.1.15

Challenge: Functional Programming - Implement map on a Prototype

Link to the challenge:

If you still have questions, about this.

Allow me to answer.

[1, 1, 2, 5, 2].myMap((element, index, array) => array[index + 1] || array[0])

The callback function has three parameters when it is being called.

this refers to the object the function was called on. i is the index of the the new array. The first parameter is the element. Then it’s an index. Then the new array.

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