/learn Functional Programming: Implement map on a Prototype

Tell us what’s happening:
Hello dear FCC community
This is my code on which I spend more than five hours but still could not understand it exactly
Until I consoled.log - callback- parameter inside the function in the 3rd line
This could have been much easier to understand if you had added the -callback-
function inside the code

Appreciate your hard work
Thank you

Your code so far


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

Array.prototype.myMap = function(callback){
var newArray = [];
// Only change code below tis line
this.forEach(function (anything) {
  newArray.push(callback(anything))
})
// Only change code above this line
return newArray;

};

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

Your browser information:

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

Challenge: Implement map on a Prototype

Link to the challenge:

We know any array will access the custom method through the prototype chain.
The this keyword, if called inside myMap's definition, will take the value of the namespace i.e s. (this isn’t that straight forward I guess).

  1. You can use this to refer to s
  2. I guess you agree push is expected
  3. And you want to push (for) each item to the new array.
    Not sure if any of this will help, but just in case.

Also,
it can be refactored like this, and may be it’s easier to read:

this.forEach(item => newArray.push(callback(item)))