Last active
August 2, 2016 08:46
-
-
Save natenault/d86d3973e830ae87f07f1142c199034d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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