Skip to content

Instantly share code, notes, and snippets.

@jpbassalot
Created March 30, 2014 21:50
Show Gist options
  • Save jpbassalot/9880440 to your computer and use it in GitHub Desktop.
Save jpbassalot/9880440 to your computer and use it in GitHub Desktop.
Map function, callback exercise
var map = function(list, callback) {
var arrList = [];
if (!list) {
throw new Error('Error, list is undefined');
}
if (list.constructor === Array && list[0].constructor === Object) {
for(var i = 0; i < list.length; i++) {
return callback(list[i]);
}
} else if (list.constructor === Array) {
for (var e = 0; e < list.length; e++) {
arrList.push(callback(list[e]));
}
return arrList;
}
};
var arr = [22,32]
map(arr, function(item) {
return item * 2; // returns [44, 64]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment