HomePremium · ₹1199
← All questions

Array polyfills: map, filter, reduce, every

Easy
Was this asked in an interview?

Very frequently asked. Write custom polyfills for map, filter, reduce, and every. reduce is the most important.

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
};
Array polyfills: map, filter, reduce, every — JavaScript Interview Question | Mentoxis