Skip to content

Instantly share code, notes, and snippets.

@kutyel
Created October 20, 2016 06:25
Show Gist options
  • Save kutyel/243ae291cbbc7dabadddef3f415b3e80 to your computer and use it in GitHub Desktop.
Save kutyel/243ae291cbbc7dabadddef3f415b3e80 to your computer and use it in GitHub Desktop.
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