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: