Skip to content

Instantly share code, notes, and snippets.

@vacas
Last active July 17, 2017 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vacas/eb42169c2241ca515ea61e1e91178637 to your computer and use it in GitHub Desktop.
Save vacas/eb42169c2241ca515ea61e1e91178637 to your computer and use it in GitHub Desktop.
// After reviewing the exercise again, I thought this revision is much cleaner and wanted to take out the extra function to not overcomplicate it
function flatten_array(input){
var result = [];
for (var i = 0; i < input.length; i++){
var flattened = input[i];
result = result.concat(Array.isArray(flattened) ? flatten_array(flattened) : flattened);
}
return result;
}
console.log(flatten_array([[1,2,[3]],4]));
console.log(flatten_array([[1,2,[3]],4, [5, 6, 8, [9, 10, [11, [12, [13, [14, 15, [16, 105, [109302940, 31409024, [9594590850]]]]]]]]]]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment