Last active
December 3, 2016 18:00
-
-
Save vikikamath/3773818644a3fb7aa0a80af1f0cf8436 to your computer and use it in GitHub Desktop.
Flatten deeply nested Array of Integers
This file contains 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
/* | |
@input: Input array to be flattened | |
@result: Empty Array | |
@return: | |
invalid input: Empty Array | |
valid input (an Array): Flattened Array | |
*/ | |
function flatten(input, result) { | |
if (Number.isInteger(input)) result.push(input); | |
if (Array.isArray(input)) { | |
input.forEach(function(element) { | |
flatten(element, result) | |
}) | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment