Skip to content

Instantly share code, notes, and snippets.

@sethschori
Forked from anonymous/myMap (Callbacks).js
Created July 31, 2016 19:50
Show Gist options
  • Save sethschori/8c9e78196d68b5acb8eeaa6658419af0 to your computer and use it in GitHub Desktop.
Save sethschori/8c9e78196d68b5acb8eeaa6658419af0 to your computer and use it in GitHub Desktop.
https://repl.it/CgPm/32 created by sethopia
// Attach a method to the Array prototype called myMap.
// It should be utilized and perform exactly as Array.prototype.map does. Use the code snippet below as a guide.
Array.prototype.myMap = function(callback) {
var returnArr = [];
for (var i = 0; i < this.length; i++) {
returnArr.push(callback(this[i],i,this));
}
return returnArr;
}
function valPlusInd(val, index) {
return val + index;
}
var arr = [1,2,3,4,5];
var mapped = arr.myMap(valPlusInd);
console.log(mapped); // Should be [ 1, 3, 5, 7, 9 ]
function addWorld(str) {
return str + ' World';
}
var arr2 = ['Hello', 'Great', 'Big'];
var mapped2 = arr2.myMap(addWorld);
console.log(mapped2); // Should be [ 'Hello World', 'Great World', 'Big World' ]
Native Browser JavaScript
>>> [ 1, 3, 5, 7, 9 ]
[ 'Hello World', 'Great World', 'Big World' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment