Skip to content

Instantly share code, notes, and snippets.

@rogerwelin
Created December 7, 2017 18:02
Show Gist options
  • Save rogerwelin/feaa1c18c3d0b5d8dd00814d0df5737a to your computer and use it in GitHub Desktop.
Save rogerwelin/feaa1c18c3d0b5d8dd00814d0df5737a to your computer and use it in GitHub Desktop.
fun-fun-function-reduce-3
// 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