Array.prototype.myMap = function(callback) {
const error = 'callback is not a function';
const isFunction = typeof callback === 'function';
if (!isFunction) {
throw new TypeError(error);
}
 
const result = new Array (this.length);
const arrayLength = this.length;
for (let i = 0; i < arrayLength; i++) {
if (i in this) {
result[i] = callback(this[i], i, this);
}
}
return result;
};
 
console.log([2, 4, 3, 4, 0].myMap(n => n % 2)); // [2, 4]
console.log(['a', 'bb', 'ccc'].myMap(s => s.length - 1)); // ['bb', 'ccc'];
console.log([1, 2, 3, , 5, 0, 3].myMap(n => n + 1)); // [];
console.log([4, 7].myMap(''));
 
Code will run automatically after changes.
Code runs in an isolated environment.