Skip to content

Instantly share code, notes, and snippets.

@marcoslhc
Last active April 6, 2017 00:31
Show Gist options
  • Select an option

  • Save marcoslhc/6e7da171efac6cdbc5b921b5db07f6a6 to your computer and use it in GitHub Desktop.

Select an option

Save marcoslhc/6e7da171efac6cdbc5b921b5db07f6a6 to your computer and use it in GitHub Desktop.
reduce sum
let arr = [1,2,3,4,5,6,7,8,9,10];
// Array.prototype.reduce takes a function (reducer) and an initial value of type B
// and returns an value of type B
let num = arr.reduce(function (accum, value) {
// The reducer function takes a value of
// type B and another value of the same type as the array (A)
// returns a value of type B;
return accum + value;
}, 0);
// the last argument is an initial value passed to the function;
console.log(num);
// 55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment