Array.prototype.map

Easy
·
45 minutes

Prompt

map is a function on the JavaScipt Array prototype that can be provided a callback, which will be called for every element in the array - the return value will be a new array containing the original elements after they have been passed through the callback.

For example:

function double(x) {
  return x * 2;
}

const myArray = [1, 2, 3, 4, 5];
const mappedArray = myArray.map(double);
console.log(mappedArray);
// [2, 4, 6, 8, 10];

You should implement your own version of map, which can be passed an array and a callback, and will return a new array with the callback run against every element. For example:

function map(array, callback) {
  // add your code here
}

const mappedArray = map([1, 2, 3, 4, 5], (x) => x * 2);
console.log(mappedArray);
// [2, 4, 6, 8, 10];

Submitting solutions

  1. Create a solution by forking one of our CodePen templates:
  2. Submit your solution here

Hints

Additional challenges

Get future questions delivered straight to your inbox for free

Sign up and receive instant access to new questions when we publish them.

We‘ll only use your information to deliver new questions and to provide you updates about our product. We‘ll never spam you or sell your information without your consent. Unsubscribe at any time.