Problem 30
Flatten nested arrays Recursion Challenge
Given an array that may contain nested arrays at any depth, return a new single-level array with all values preserved in order.
Function Signature
flatten(array)
Parameters
array— an array that may contain values or nested arrays at arbitrary depth.
Output
Return a flat array containing all non-array values from the input.
Constraints
- The function must use recursion.
- Do not use
Array.prototype.flat. - The function should accept exactly one argument.
Examples
flatten([[1], [2, 3], [[4]], 5]) → [1, 2, 3, 4, 5]
flatten([3, [0, [34, [7, [18]]]]]) → [3, 0, 34, 7, 18]
flatten([[1], [2, [], 3], [], [[4]], 5]) → [1, 2, 3, 4, 5]