Created
October 9, 2016 23:03
-
-
Save mhfowler/24d1ccb3cb4c034bbf735a3a9b094d7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
flattenArr(arr): | |
# base case | |
## how to check if a variable is an array http://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript | |
if (arr.constructor != Array) { | |
return [arr]; | |
} | |
# recursion | |
else { | |
foundElements = [] | |
for (var i = 0; i < arr.size; i++) { | |
elts = flattenArr(arr[i]) | |
foundElements = foundElements.concat(elts) | |
} | |
return foundElements; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment