Paytm Interview Question

Create functionality of map() using pure Javascript

Interview Answer

Anonymous

Jul 3, 2020

if (!Array.prototype.customMap) { Array.prototype.customMap = function (callback) { let index, len = this.length; const newArray = []; for (index = 0; index < len; index++) { newArray.push(callback(this[index], index, this)); } return newArray; } } const doubleArrayElem = [1,2,3,4,5,6].customMap(function (elem, index, arr) { return elem * 2; }); console.log(doubleArrayElem);