Home
← All questions

Array polyfills: map, filter, reduce, every in JavaScript

EasyPremiumJavaScript

JavaScript interview practice: Write custom polyfills for map, filter, reduce, and every.

Problem

Implement polyfills for the core array methods as Array.prototype.myMap, myFilter, myReduce, and myEvery. Match the native callback signature (element, index, array) and, for reduce, the optional initialValue.

Array.prototype.myMap = function (callback, thisArg) {
  // your code
};

Array.prototype.myFilter = function (callback, thisArg) {
  // your code
};

Array.prototype.myReduce = function (callback, initialValue) {
  // your code
};

Array.prototype.myEvery = function (callback, thisArg) {
  // your code
};
Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium