Problem 41

Nested Array Depth Recursion Challenge

Given an array that may contain nested arrays, return the maximum nesting depth. A flat array (no nested arrays) has a depth of 1.

Function Signature

arrayDepth(array)

Parameters

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

Output

Return a number representing the maximum depth of nesting.

Constraints

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

Examples

arrayDepth([])                          → 1
arrayDepth([1, 2, 3])                   → 1
arrayDepth([1, [2], 3])                 → 2
arrayDepth([1, [2, [3]], 4])            → 3
arrayDepth([[[[[1]]]]])                 → 5
arrayDepth([1, [2, [3, [4]]], [[5]]])   → 4