DEV Community

Discussion on: Implementing our own Array.map() method in javascript

Collapse
 
akanshgulati profile image
Akansh

@diliptwit291 The above implementation fails for

[1,2,3, null, , 4].mymap(a => +a);
Enter fullscreen mode Exit fullscreen mode

Gives, different results from map

.mymap => [1, 2, 3, 0, NaN, 4]
.map => [1, 2, 3, null, undefined, 4]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ashwaniweb profile image
Worried Indian • Edited

Array.prototype.myMap= function (callback) {
let result = this;
for (let index = 0; index < this.length; index++) {
if (this[index] === undefined) {
break;
}
result[index] = callback(this[index], index, this);
}
return result;
};