Flatten an Array to a Depth

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write myFlat(items, depth) without using the built-in flat or flatMap. Return a new array in which nested arrays are opened up to depth levels deep. depth defaults to 1, a depth of 0 returns a shallow copy, and Infinity opens every level. Only arrays are opened: strings, objects and every other value stay as they are. Don't change items or any array inside it. The arrays in the checks have no gaps.

Your code must pass

  • Flattens one level by default
  • Flattens as many levels as depth
  • Flattens every level with Infinity
  • Returns a copy for depth 0 and leaves the input alone
  • Works without the built-in flat