Last active
April 6, 2017 00:31
-
-
Save marcoslhc/6e7da171efac6cdbc5b921b5db07f6a6 to your computer and use it in GitHub Desktop.
reduce sum
This file contains hidden or 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
| 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