Help me on: Implement map on a Prototype challenge

Tell us what’s happening:
Hello everyone, i found the solution on internet, but in fact i can’t understand what is happening with this this.forEach(item => newArray.push(callback(item)));

I can’t understand why using this in this case and how we can access item, if in the parameters we only have callback and not callback(item)

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
this.forEach(item => newArray.push(callback(item)));
// Add your code above this line
return newArray;

};

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

Challenge: Implement map on a Prototype

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

this is the array on which the myMap method is used

callback is the parameter of myMap, it is a function, and to call it you do it like any function

forEach is an array method, takes a function as parameter (called callback)

item is the parameter of the forEach callback

1 Like

Thank you for your advises related to the forum “rules”.

And thank you very very much for your explanation, now i can understand clearly what is happening in the code.