TylerRosen
7/3/2017 - 6:49 PM

Write a function called add which takes in an array and returns the result of adding up every item in the array. add([1,2,3]) -> 6

Write a function called add which takes in an array and returns the result of adding up every item in the array. add([1,2,3]) -> 6

// Imperative

function add (arr) {
  let result = 0
  for (let i = 0; i < arr.length; i++){
    result += arr[i]
  }
  return result
}

// Declarative

function add (arr) {
  return arr.reduce((prev, current) => prev + current, 0)
}