Tell us what’s happening:
Describe your issue in detail here.
I tried submitting my code and it passed all the tests except for when it was passed this one . [1, 1, 2, 5, 2].myMap((element, index, array) => array[i + 1] || array[0]) should return [1, 2, 5, 2, 1] .
when I console log it is says i is not defined. I looked up the hints and the code written there is the same as the code I have written. Is this a mistake or am I missing something.
Your code so far
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
Challenge: Functional Programming - Implement map on a Prototype
You are assuming in your code that the callback function works on an element.
This is true sometimes
But in this specific testcase, the callback has to work when given an element, an index and an array.