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...
Markdown supported · ⌘↵ to post
D
D personal tech nuggets
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 }