Sun Jan 05 2020 19:37:35 GMT+0000 (UTC)

Posted by @bezequi #javascript #webdev #arrays #math

const arrSum = arr => arr.reduce((a,b) => a + b, 0) // arrSum([20, 10, 5, 10]) -> 45

This function makes use of the JavaScript reduce() method to reduce our array to a single value. It does this by applying a function to each element in the array. The function that we pass as the first parameter of the reduce method receives two parameters, a and b. In this code, a is our accumulator. It will accumulate our sum as our function works. b is the current value being processed. The second parameter of the reduce method is the initial value we wish to use. We’ve set our initial value to zero which allows us to use empty arrays with our arrSum functions. In other words, we are simply going to start with zero and one by one add each value of the array to our initial value until we’ve looped through the entire array. When done, the accumulator value will be returned

https://codeburst.io/javascript-arrays-finding-the-minimum-maximum-sum-average-values-f02f1b0ce332