Functional Programming - Implement map on a Prototype

Tell us what’s happening:
Describe your issue in detail here.
I still do not understand why we have to use “this” in the code, like this.length or this[i], what does this in these two examples mean,please explain it to me. Thank you.

  **Your code so far**
// The global variable
const s = [23, 65, 98, 5];
// console.log(s.length)

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

}
// Only change code above this line
return newArray;
};

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

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Functional Programming - Implement map on a Prototype

Link to the challenge:

Up until now, you have seen functions where you might might pass an array as an argument, do something with the array, and return something else (maybe an array). As you have already seen in the challenge that introduces the map method, you call a method on the array which returns a value. This is only possible because a map method has been defined for all arrays as part of Array.prototype.

The callback function you create must reference the array you call with the new myMap method you are to create. The only way to reference the array is to use the this keyword. In this case, this is a reference to the array on which the method is called.

The lesson does not cover what this is, so this is freeCodeCamp’s way to get you to perform some external research (something you do daily as a software developer) to solve this challenge.

1 Like