Flatten Deeply Nested Array (JS) - Easy 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 []
/**
 * Flattens a multi-dimensional array up to a specified depth.
 * @param {Array} arr - The multi-dimensional array to flatten.
 * @param {number} n - The depth level to flatten the array.
 * @return {Array} - The flattened array.
 */
var flat = function(arr, n) {
    // Initialize the result array
    let result = [];

    // Helper function to recursively flatten the array
    function flattenHelper(currentArr, currentDepth) {
        // Iterate through each element in the current array
        for (let item of currentArr) {
            // If the current depth is less than n, check if the item is an array
            if (currentDepth < n && Array.isArray(item)) {
                // Recursively flatten the sub-array
                flattenHelper(item, currentDepth + 1);
            } else {
                // Otherwise, push the item to the result
                result.push(item);
            }
        }
    }

    // Start the flattening process
    flattenHelper(arr, 0);

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