Problem 3

Sum Integers in Array Recursion Challenge

Given an array of integers that may contain nested arrays at any depth, return the sum of all integers.

Function Signature

arraySum(array)

Parameters

  • array — an array that may contain integers or nested arrays of integers at arbitrary depth.

Output

Return a number representing the sum of all integers found at any nesting level.

Constraints

  • The function must use recursion.
  • Do not use Array.prototype.flat or any flatten utility.
  • The function should accept exactly one argument.
  • The input array must not be mutated.

Examples

arraySum([1, [2, 3], [[4]], 5])          → 15
arraySum([[12, [[34], [56]], 78]])        → 180
arraySum([[-1], [-2, -3], [[-4]], -5])   → -15
arraySum([])                             → 0
arraySum([4])                            → 4

Edge Cases

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