Write your own version of Array.prototype.flat. flatten(items, depth) returns a new array where nested arrays are opened up to depth levels deep: arrays inside items are replaced by their items, arrays inside those are opened too while levels remain, and arrays deeper than depth are kept as they are.
depth = 0 returns a shallow copy. Non-array values (numbers, strings, objects, null) are never opened.
Input: items = [1,[2,[3,[4]]],5], depth = 2 Output: [1,2,3,[4],5]
Two levels are opened, so [4], three levels deep, stays an array.
Input: items = [1,[2,[3]]], depth = 1 Output: [1,2,[3]]
Only the first level is opened.
items has at most 10^4 values in total; nesting is at most 100 levels.0 <= depth <= 100Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #743 Flatten a List to a Depth. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.