Skip to content

Instantly share code, notes, and snippets.

@hai5nguy
Created February 12, 2019 17:31
Show Gist options
  • Save hai5nguy/79c1e0e323a9a9695696f0c8923b6918 to your computer and use it in GitHub Desktop.
Save hai5nguy/79c1e0e323a9a9695696f0c8923b6918 to your computer and use it in GitHub Desktop.
hai nguyen - citrusbyte coding question
var sampleArray = [[1,2,3],[[4],[5,6], [7,[8],9]], [10, 11], [12, [13, 14]]]
function flatten(array) {
var result = [];
recurseFlatten(array, result);;
return result;
}
function recurseFlatten(array, result) {
for (var i = 0; i < array.length; i++) {
var item = array[i];
if (Array.isArray(item)) {
recurseFlatten(item, result);
} else {
result.push(item);
}
}
}
console.log(flatten(sampleArray))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment