Best JavaScript and TypeScript Solution for Beginner 💛
Code\njavascript []\nvar flat = function (arr, n) {\n    if (n === 0) {\n        return arr;\n    }\n    \n    let answer = [];\n    \n    arr.forEach(element =
# Code\n```javascript []\nvar flat = function (arr, n) {\n    if (n === 0) {\n        return arr;\n    }\n    \n    let answer = [];\n    \n    arr.forEach(element => {\n        if (n > 0 && Array.isArray(element)) {\n            answer.push(...flat(element, n - 1));\n        } else {\n            answer.push(element);\n        }\n    });\n    \n    return answer;\n};\n\n```\n```typescript []\nconst flat = function (arr: any[], n: number): any[] {\n    if (n === 0) {\n        return arr;\n    }\n    \n    let answer: any[] = [];\n    \n    arr.forEach(element => {\n        if (n > 0 && Array.isArray(element)) {\n            answer.push(...flat(element, n - 1));\n        } else {\n            answer.push(element);\n        }\n    });\n    \n    return answer;\n};\n\n```
['TypeScript', 'JavaScript']