Array.prototype.map

In this coding challenge, you will be implementing a custom myMap method for JavaScript arrays. The myMap method should behave similarly to the built-in map method, but should be implemented from scratch without using the built-in method.
The myMap method should take a callback function as its argument, and should apply the callback function to each element in the array, in the order in which they appear. The callback function should take three arguments: the current element, the index of the current element, and the array on which myMap was called. The callback function should return a value, which will be used to populate the new array returned by myMap.
The myMap method should return a new array containing the values returned by the callback function for each element in the original array. The new array should have the same length as the original array, and its elements should be in the same order as the original array.
Your implementation should be written as an extension to the Array.prototype, so that the myMap method can be called on any array.

Instructions

  • You may not use the built-in map method or any other array methods in your implementation of myMap.
  • Your implementation should not modify the original array.

Example test cases

const numbers = [1, 2, 3, 4, 5]; const squares = numbers.myMap(num => num * num); console.log(squares); // [1, 4, 9, 16, 25]
Adding your container request
Getting your dedicated container
Connecting to your container
Setting up your editor
Finalizing your playground
TerminalEditorBrowser