I don't understand how a prototype function (1) returns a array and then that array is iterated to the argument of the function (1)?

Tell us what’s happening:
Describe your issue in detail here.
The ’ Mymap ’ function takes a function as argument. Then it returns a array.
The part I don’t understand is How the array returned by Mymap function is iterated into the function that was the argument of the Mymap function ???

  **Your code so far**

// The global Array
var s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback) {
var newArray = [];

// Add your code below this line
for (let i = 0; i < this.length; i++) {
  newArray.push(callback(this[i]));
}
// Add your code above this line

return newArray;
};

var new_s = s.myMap(function(item) {
return item * 2;
});
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36

Challenge: Implement map on a Prototype

Link to the challenge:

Look at what the myMap function is doing in the for loop:

newArray.push(callback(this[i]));

It is calling the callback function for each element in the array. So the function you passed into myMap is being applied to every element in the array.

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