waynedunkley
4/3/2016 - 12:37 AM

The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single valu

The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.

var scores = [89, 76, 47, 95];
var initialValue = 0;

var reducer = function (accumulator, item) {
  return accumulator + item;
};

var total = scores.reduce(reducer, initialValue); 
// total = 307

/*------- or ----------*/

var total = scores.reduce(function(accumulator, item){
  return accumulator + item;
}, 0);
// total = 307