Javascript/Frontend DS&A Interview questions
No Frontend interview is complete without DS/Logic question. Interviewers usually look at your problem solving skills in this round. In this series of blogs, we will go through most frequently asked Data Structure and Algorithms questions in Javascri...
devsimplified.hashnode.dev2 min read
D
Here's my solution with maxDepth:
function flatten(arr, maxDepth=Infinity, currDepth = 1){ let res = [] for (let i = 0; i < arr.length; i++) { if (!Array.isArray(arr[i]) || currDepth > maxDepth) { res.push(arr[i]) } else { res = res.concat(flatten(arr[i], maxDepth, ++currDepth)) } } return res }