Created
December 25, 2011 15:50
-
-
Save sri-rang/1519448 to your computer and use it in GitHub Desktop.
Functional Programming in JavaScript - 5
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 forEach = function (list, action) { | |
for (var i = 0; i < list.length; i++) { | |
action(list[i]); | |
} | |
}; | |
var map = function (mappingFunction, list) { | |
var result = []; | |
forEach(list, function (item) { | |
result.push(mappingFunction(item)); | |
}); | |
return result; | |
}; | |
var doubleIt = function (item) { | |
if (typeof item === "number") { | |
return item * 2; | |
} | |
}; | |
console.log(map(doubleIt, [1, 2, 3, 4, "WTF"])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment