Skip to content

Instantly share code, notes, and snippets.

@KinoAR
Last active Jul 12, 2017
Embed
What would you like to do?
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