Functional Programming - Implement map on a Prototype

Question 1) Based on the instructions and code provided in the problem, how would I know the callback function would need these three parameters: item, index, originalArray?

Question 2) Why is the original array referred to as “originalArray” and not this keyword in the arguments?

Your code so far

Array.prototype.myMap = function(callback) {
  const newArray = [];
  // Only change code below this line
  this.forEach((item, index, originalArray) => newArray.push(callback(item, index, originalArray)));
  // Only change code above this line
  return newArray;

Challenge: Functional Programming - Implement map on a Prototype

Link to the challenge:

  • callback functions are those “that happens after some event takes place or happening something”, which means you would know before what that “callback needs to do” and “what it needs” to carry out that action
  • you can call these “arguments” whatever you like, but its for your “understanding” that they are named so, for instance here, “map” method takes in “3” (or probably more) arguments in it, first of which is “item of that array” and then “index” and followed by “array” in question itself

hope this was helpful, happy learning :slight_smile:

1 Like

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