Easy JavaScript Solution
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# 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
};
```
['JavaScript']