Functional Programming - Implement map on a Prototype

Tell us what’s happening:
I managed to find both solutions for the challenge from the forums but I can’t wrap my head around how this works.

So in the beginning my (a) inside the forEach refers to the array index 0 and forEach iterates through the whole array.
Then arrow function is then used where (a) is the target of the operation.
The result of execution of callback on (a) is then pushed into newArray.

If I understood it right, keyword this is used to refer to the array part of callback and the array part is calling for the function myMap(item => item * 2);

So how does the computer know that I want to apply myMap(item => item * 2); to (a) instead of trying to apply the whole [23, 65, 98, 5, 13].myMap(item => item * 2); to (a) as callback as an argument has [23, 65, 98, 5, 13].myMap(item => item * 2); inside it.

I tried to read about how this works but I can’t understand how

newArray.push(callback(a)));

works as the callback now refers to all that it has inside it, not only myMap(item => item * 2); part.

Your code so far

Array.prototype.myMap = function(callback) {
  const newArray = [];
  // Only change code below this line
  this.forEach((a) => newArray.push(callback(a)));

  /*for (let i = 0; i < this.length; i++) {
    newArray.push(callback(this[i]));
  }*/
  console.log(newArray + "123")
  // Only change code above this line
  return newArray;
};

Array.prototype.myMap();

Your browser information:

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

Challenge: Functional Programming - Implement map on a Prototype

Link to the challenge:

I think you need to look at that entire line:

this.forEach((a) => newArray.push(callback(a)));

Do you understand how forEach works?

It sounds like you do. The variable a represents the current item that is being iterated in the array using forEach.

Because that’s how forEach works. It goes through each element in the array one at a time and passes the item into a function you have provided. The function you provided is:

(a) => newArray.push(callback(a))

forEach is calling that function for each item in the array and passing the value of the item into your function as the first argument, which you have named a.