reduceSum

Easy · Functional Programming Concepts · 3 test cases

Given an array of numbers, calculate their sum using the `reduce` method.

Examples

reduceSum([1, 2, 3, 4, 5]) → 15
reduceSum([10, -2, 7]) → 15

Starter code

function reduceSum(arr) {

}

Complexity of the optimal solution

Time O(n), space O(1). The `reduce` method iterates over each of the 'n' elements in the array once to compute the final sum. The accumulator uses constant extra space.

Solve reduceSum in the browser