Functional Programming - Implement map on a Prototype

I have this so far. It took a long time to understand what was being asked here.

This code returns the correct answer for 2 of the tests except this one :
[1, 1, 2, 5, 2].myMap((element, index, array) => array[index + 1] || array[0]) should return [1, 2, 5, 2, 1]

I honestly don’t even understand whats happening in that test. This problem is making me feel too stupid to code honestly. I have no idea where to go from here. I’ve tried researching “this”. went over objects again. Went over .map().

I’m completely lost.

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;
};

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36

Challenge: Functional Programming - Implement map on a Prototype

Link to the challenge:

If you check the last Challenge, the callback function here may use up to 3 arguments

Oh ok.

I just changed it to callback(this[i], i, this) and it seemed to work. I think i understand whats going on but I’m going to research more on callback functions to make sure i get it.

Thanks!

1 Like

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