Functional Programming - Implement map on a Prototype

I don’t understand the second line of this function (the bit starting with newArray.push). Why can’t we just push I onto each element of the array like a normal for statement, whats all this about a callback? Im confused…

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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Functional Programming - Implement map on a Prototype

Link to the challenge:

Hi there. Callback functions are an important topic. Notice how what is being pushed is the result of a function, not just a specified item. The function that is passed to the push method is returning its result to the push. It is used here bc you are replicating what map does without using map

so the callback is the function that the map is applying to each element?

1 Like

Okay I get it now, thanks

Yes it is the function applied to each element of original array to make it ACT like a map function as it pushes elements to new array

1 Like

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