Array flattening

JavaScript
Medium

Objective

Write a function to flatten an array of arrays

Requirements

  1. The function should be called flatten

  2. It should take a single parameter, arrays, which is an array of arrays

  3. It should return a new array

  4. The function should not use the built-in array methods flat or flatMap

Check your solution

Tests

Function name is flatten
flatten([1,2,[3,4]])Expected:[1,2,3,4]
flatten([0,"ipsum",[1,"ipsum",[2]]])Expected:[0,"ipsum",1,"ipsum",2]

Evaluation Criteria

If I gave you this test, here's what I'd be looking for:

  1. Understanding of recursion

    1. The test array contains nested arrays, so you'll need to use recursion to handle them

  2. Problem-solving skills

    1. This would be an easy test if I allowed you to use the `flat` method, by telling you not to use it - I'm looking to see if you can think around it

  3. Good understanding of array methods

    1. Answering this question will require a decent level of understanding of the default array methods.

  4. Edge cases

    1. I'm not specifically asking for it, but bonus points if your solution can handle empty arrays

Click to reveal