Functional Programming - Implement map on a Prototype

Tell us what’s happening:
I need help understanding the difference between my code and the solution code. From my understanding, these two codes should work the same, but for exercises apparently, it doesn’t. Here is my code and the solution code for your review.

Thank you in advance

  **My code version (which is apparently wrong)**
// The global variable
const s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback) {
const newArray = [];
// Only change code below this line
for (let i=0; i<callback.length; i++) {
  newArray.push(callback[i]);
}
// Only change code above this line
return newArray;
};

const new_s = s.myMap(function(item) {
return item * 2;
});
  **The Solution Code**
// The global variable
const s = [23, 65, 98, 5];

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]));
}
// Only change code above this line
return newArray;
};

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

As you can notice, both codes are identical, but the solution code uses the “This” keyword.

  **Your browser information:**

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

Challenge: Functional Programming - Implement map on a Prototype

Link to the challenge:

You do understand that callback is a function and not an array?

Not the type of answer I am looking for

we cannot give you the answer we can only guide you or tell you what to write not the answer other wise you would not learn

But do you understand what I asked?

You asked why your code doesn’t work and was told you are using the callback function as an array, which it isn’t, it’s a function.

Add these logs to the function

Array.prototype.myMap = function (callback) {
  console.log(callback.toString())
  console.log(this)
};

Due to the lack of answers I received, I searched on the web trying to understand why my code didn’t work, and here is what I have found in case someone has the same question as I did. Here is the explanation of my question:

In my code when I am trying to inspect the callback parameter, this is not the actual array but the function assigned in const new_s = s.myMap(**This function**); Thus, when I am trying to push the callback[i] in my newArray this do not inset the actual number but an undefined value to it.

On the other hand, the This operator is the array because when the variable s calls the prototype myMap, this assigns its value to the prototype itself. In other words, s.myMap transfers its value to Array.prototype.myMap, which can be called with the This operator; Therefore, in this scenario, the operator this[i] represents the values of the array.

The challenge instructions did state:

The Array instance can be accessed in the myMap method using this