Ad

Get sum of elements of array using Array.reduce function

/* Array.reduce(
 * (accumulator, currentValue, currentIndex, array) => {
 * return accumulator + currentValue;
 * },
 * initialValue
 * );
 */
function sumOfElements(arr) {
  if(arguments.length === 0)
    arr = [1,2,3,4,5];
  
  var total = arr.reduce( (sum, currentValue) => {return sum + currentValue}, 0 );
  
  console.log(total);
  return total;
}