Faster recursive solution without spread operator
IntuitionThe initial thought is to create a solution that flattens a nested array up to a specified depth. The problem requires recursively processing array ele
# Intuition
The initial thought is to create a solution that flattens a nested array up to a specified depth. The problem requires recursively processing array elements, where we need to:

- Handle nested arrays by diving deeper when depth allows
- Collect non-array elements into a result array
- Stop recursion when either we hit the depth limit or encounter non-array elements

# Approach
The solution uses a recursive approach with these steps:

1. Create an empty result array flattenArr to store flattened elements
2. Handle base case: if depth (n) is 0, return the array as-is
3. Use a helper function handleFlattening that:
    - Iterates through each element in the array
    - For array elements: recursively calls itself with decremented depth if n > 0
    - For non-array elements: adds them to the result array
4. Return the flattened result

# Complexity
- Time complexity: O(n ∗ m)
    - Where n is the total number of elements across all nested arrays
    - m is the maximum depth of recursion
    - Each element is processed once, but we may need to traverse through multiple levels

- Space complexity: O(m)
    - Where m is the maximum depth of recursion
    - Space is used by the recursive call stack
    - The output array doesn't count toward space complexity by convention

# Code
```javascript []
/**
 * @param {Array} arr
 * @param {number} depth
 * @return {Array}
 */
var flat = function (arr, n) {
    const flattenArr = []

    if (!n) {
        return arr
    }

    const handleFlattening = (arr, n) => {
        for (let i = 0; i < arr.length; i++) {
            if (Array.isArray(arr[i]) && n) {
                handleFlattening(arr[i], n - 1)
            } else {
                flattenArr.push(arr[i])
            }
        }
    }

    handleFlattening(arr, n)

    return flattenArr
};
```
['JavaScript']