Created
October 20, 2016 06:25
-
-
Save kutyel/243ae291cbbc7dabadddef3f415b3e80 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const addNumers = (a, b) => a + b; | |
// Takes the values of an array and returns the total. Demonstrates simple | |
// recursion. | |
const totalForArray = (arr, currentTotal) => { | |
currentTotal = addNumers(currentTotal + arr.shift()); | |
if (arr.length) { | |
return totalForArray(currentTotal, arr); | |
} else { | |
return currentTotal; | |
} | |
}; | |
// Or you could just use reduce! | |
const totalForArray = arr => arr.reduce(addNumers); | |
// Should really be called divideTwoNumbers | |
const average = (total, count) => count / total; | |
const averageForArray = arr => average(arr.length, totalForArray(arr)); | |
// Gets the value associated witht the property of an object. Intended for | |
// use with a collection method like map, hence the generator. | |
const getItem = propertyName => item => item[propertyName]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment