Skip to content

Instantly share code, notes, and snippets.

@natenault
Last active August 2, 2016 08:46
Show Gist options
  • Save natenault/d86d3973e830ae87f07f1142c199034d to your computer and use it in GitHub Desktop.
Save natenault/d86d3973e830ae87f07f1142c199034d to your computer and use it in GitHub Desktop.
var each = function(collection, callback) {
if( Array.isArray(collection) ) {
for( var i = 0; i < collection.length; i++ ) {
collection[i] = callback( collection[i] );
}
} else {
for( var prop in collection ) {
collection[prop] = callback( collection[prop] );
}
}
};
var map = function(collection, callback) {
// Create new array so that old array is preserved
var result = [];
// Add elements from old array to new array
for( var i = 0; i < collection.length; i++ ){
result[i] = collection[i];
}
// Pass new array through "each" function
each(result, callback );
// Return the value of the new array
return result;
};
var allottedMinutes = [15, 20, 32];
var double = function (num) {
return num * 2;
};
var doubledAllottments = map(allottedMinutes, double);
console.log(doubledAllottments); // logs [30, 40, 64]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment