Functional Programming - Implement map on a Prototype

Tell us what’s happening:

Hi campers, I’m confused about the use of this in this exercise as well as the 3 args in the callback function. I have peeked quickly at the solution as I wasn’t getting anywhere, and noticed that that the call back function takes 3 arguments, which I’ve not seen before. Looking at an previously answered question, it says that is normal, but I haven’t seen its implementation used before and was wondering if someone might be able to explain what is happening, especially when used with this

Your code so far

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

//Why just this, why not this.Array?
 for (let i = 0; i < this.Array.length; i++){

   /*I know there should be 3 args in here, but I don't know how or why they are implemented as they are*/

   newArray.push(callback(this.Array[i]))
   console.log(newArray)
 } 
  // Only change code above this line
  return newArray;
};

Your browser information:

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

Challenge Information:

Functional Programming - Implement map on a Prototype

1 Like

Hello,

From what I understand the 3 arguments you are talking about that the callback function take are the current item in the array, another argument called index ususally & the last one is arr or array.
Well, if that is what you are asking about, you should know index & arr are optional ones passed to the callback function, the first contains the index of the current element in the array while the second is a reference to the array itself that called the map functio in the first place

The MDN page on map explains its API


As for the this keyword, that can get a little more complicated.

For method invocations, it is usually safe to assume that this inside the method is whatever is “left of the dot” (this in a method invocation).

object.someMethod() (object is “left of the dot”)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.