Problem 2

Sum of Integers Recursion Challenge

Given an array of integers, return the sum of all elements in the array.

Function Signature

sum(array)

Parameters

  • array — an array of integers. May contain positive, negative, or zero values.

Output

Return a number representing the sum of all integers in the array.

Constraints

  • The function must use recursion.
  • The function should accept exactly one argument.
  • The input array must not be mutated.

Examples

sum([1, 2, 3, 4, 5, 6]) → 21
sum([-1, -2, -3])        → -6
sum([1, -2, 3, -4])      → -2
sum([])                  → 0
sum([4])                 → 4

Edge Cases

  • An empty array should return 0.
  • An array with a single element should return that element.