Easy Explanation
Explanation:Helper Function:The recursive helper function flattenHelper takes the current array and its depth as arguments.
If the current depth is greater than
# Explanation:
### Helper Function: 
The recursive helper function flattenHelper takes the current array and its depth as arguments.

- If the current depth is greater than or equal to the specified n, the function stops recursing and returns the array as-is.
- Otherwise, it iterates through each element of the array. If an element is an array, it flattens it by making a recursive call with an increased depth. If it's not an array, it adds the element to the result.
### Base Case: 
If the current depth equals or exceeds n, the recursion stops.

### Recursive Case: For each element in the array, it checks whether it’s an array:

If it is, it recursively calls the helper function.
Otherwise, it adds the element to the flattened result.
### Output: The final flattened array is returned.

This approach avoids using the Array.flat() method and handles the depth-based flattening manually. It is efficient and adheres to the constraints.

# Code
```javascript []
/**
 * @param {Array} arr
 * @param {number} n
 * @return {Array}
 */
var flat = function (arr, n) {
    // Helper function to recursively flatten the array
    const flattenHelper = (array, currentDepth) => {
        if (currentDepth >= n) {
            return array; // If depth limit is reached, return array as is
        }

        const result = [];
        for (const element of array) {
            if (Array.isArray(element)) {
                // Recursively flatten nested arrays
                result.push(...flattenHelper(element, currentDepth + 1));
            } else {
                // Add non-array elements directly
                result.push(element);
            }
        }

        return result;
    };

    return flattenHelper(arr, 0); // Start with depth 0
};

// Example usage:
const arr1 = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]];
console.log(flat(arr1, 0)); // Output: [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
console.log(flat(arr1, 1)); // Output: [1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]
console.log(flat(arr1, 2)); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
```
['JavaScript']