Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@KinoAR
Last active July 12, 2017 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KinoAR/e675c196d5c77c207e250ce371a049ca to your computer and use it in GitHub Desktop.
Save KinoAR/e675c196d5c77c207e250ce371a049ca to your computer and use it in GitHub Desktop.
An example of using reduce on an array of prices.
/* Reduce Example
* Go through each element and return a value when combining the previous element with the curr element
*/
const shoppingListPrices = [10.21, 20.33, 40, 33.5, 5, 2, 7, 9, 9.99, 10.99];
/* ES6 */
const totalPrice = shoppingListPrices.reduce((start, curr) => start + curr, 0);
/* ES5 */
const totalPrice2 = shoppingListPrices.reduce(function (start, curr) {
return start + curr;
}, 0);
console.log("Shopping List",shoppingListPrices); //ShoppingList [10.21, 20.33, 40, 33.5, 5, 2, 7, 9, 9.99, 10.99]
console.log(totalPrice); //148.02
console.log(totalPrice2); //148.02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment