Created
December 7, 2017 18:02
-
-
Save rogerwelin/feaa1c18c3d0b5d8dd00814d0df5737a to your computer and use it in GitHub Desktop.
fun-fun-function-reduce-3
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
// mission: summarize the amount | |
// reduce is multi tool on list transformation | |
var orders = [ | |
{amount: 250}, | |
{amount: 400}, | |
{amount: 100}, | |
{amount: 325} | |
] | |
// similar to erlang tail recursion | |
// if we do a console.log inside the function it becomes more clear | |
var totalAmt = orders.reduce(function(sum, order) { | |
console.log("hello", sum, order) | |
return sum + order.amount | |
}, 0) | |
// using arrow functions | |
var totalAmt2 = orders.reduce((sum, order) => sum + order.amount, 0) | |
console.log(totalAmt) | |
console.log(totalAmt2) | |
/* solving the problem using for loop | |
var totalAmt = 0 | |
for (var i = 0; i < order.length; i++) { | |
totaltAmt += orders[i].amount | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment