← Back to challenges

Recursion: Flatten the Curves

JavaScriptHardrecursionarrays

Instructions

The nesting of arrays can be viewed indirectly as curves and barriers of the real data embedded in arrays, thus, defeats the very purpose of directly accessing them thru indexes and slices. In this challenge, a function is required to flatten those curves (i.e. level, iron, compress, raze, topple) and expose those data as a single array and not as an array of arrays.

Examples

flatten([[[[[["direction"], [372], ["one"], [[[[[["Era"]]]], "Sruth", 3337]]], "First"]]]])
➞ ["direction", 372, "one", "Era", "Sruth", 3337, "First"]

flatten([[4666], [5394], [466], [[["Saskia", [[[[["DXTD"]], "Lexi"]]]]]]])
➞ [4666, 5394, 466, "Saskia", "DXTD", "Lexi"]

flatten([[696], ["friend"], ["power"], [[[["Marcus"]]]], ["philus"]])
➞ [696, "friend", "power", "Marcus", "philus"]

flatten([[["deep"], [[["ocean"]]], [["Marge"]], ["rase", 876]]])
➞ ["deep", "ocean", "Marge", "rase", 876]

Notes

  • There are no empty arrays to handle.
  • You're expected to solve this challenge using a recursive approach.
  • A collection of challenges about recursion can be found via this link.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.