Functional Programming - Implement map on a Prototype mistake?

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

Link to the challenge:

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.

Sorry I guess I am a little confused by that, could you explain a little bit further?

here, add this code to the end of your file
console.log([1, 1, 2, 5, 2].map((element, index, array) => array[index + 1] || array[0]));

This will log the return value of calling map with a callback function that takes 3 parameters.

Your function needs to behave the same way.

yes the testcase has a typo so I changed it slightly to allow it to work

Okay that makes sense. In this case do I just skip this step or is there away to fix it

It is just being shown using the wrong index. It has nothing to do with the actual test.

You aren’t passing the test because your solution does not handle callback functions with 3 arguments.

gotcha thanks for your help!

I made a PR for the typo

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