Problem Solving - Array Flatten
In Javascript we know that we can nest arrays inside of other arrays. For example, the following is a valid array in JavaScript
but you can imagine a case when you'd like to flatten an array, decompose it into just an array of elements, so...
would become
Challenge 1
Create a JavaScript file called flatten.js
and implement the function below that flattens an array. For now, don't worry about arrays that are more than one level deep.
HINT: There might be something useful in here.
Challenge 2
As we know, arrays in JavaScript can be nested as deeply as we want. So the input could be...
or even something like...
Therefore, our function needs to be able to handle an array of any depth. Create a new function called flatten2
that flattens deeply nested arrays. So, both of the examples should be flattened to [1, 2, 3, 4, 5, 6, 7]
.
Last updated