Created
March 17, 2018 08:23
-
-
Save waimyokyaw/07e23dd3df204df28d6999fd78937f8a to your computer and use it in GitHub Desktop.
Sum elements of array using javascript
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
const array1 = [1, 2, 3, 4]; | |
const reducer = (accumulator, currentValue) => accumulator + currentValue; | |
// 1 + 2 + 3 + 4 | |
console.log(array1.reduce(reducer)); | |
// expected output: 10 | |
// 5 + 1 + 2 + 3 + 4 | |
console.log(array1.reduce(reducer, 5)); | |
// expected output: 15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment