Last active
July 12, 2017 20:10
-
-
Save KinoAR/e675c196d5c77c207e250ce371a049ca to your computer and use it in GitHub Desktop.
An example of using reduce on an array of prices.
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
/* 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