Skip to main content

JavaScript Array flat — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Array flat lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Flatten one level

    easy10 min

    Flatten a nested array and join the result with commas.

  2. 2

    Flatten two levels

    easy10 min

    Flatten an array nested two deep and show the length.

  3. 3

    Flatten completely

    easy10 min

    Use Infinity to flatten however deep the nesting goes.

  4. 4

    Split into words

    easy10 min

    Use flatMap to split each sentence into words and join with commas.

  5. 5

    Merge groups of names

    medium10 min

    Flatten an array of groups and show the total number of people.

Find the bug(5)

  1. 1

    Fix the default depth

    easy8 min

    One level is not enough for an array nested two deep.

  2. 2

    Fix map used instead of flatMap

    easy8 min

    map leaves each result nested inside its own array.

  3. 3

    Fix the result of flat being ignored

    easy8 min

    flat returns a new array; it does not change the original.

  4. 4

    Fix the fixed depth on unknown nesting

    easy8 min

    The nesting is deeper than the depth given.

  5. 5

    Fix concat used on nested groups

    easy8 min

    The groups need flattening into one list of people.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace