Created
January 23, 2019 22:08
-
-
Save benravago/b7ef110adbddada617a02169093fdefa to your computer and use it in GitHub Desktop.
flatten an arbitrary integer array
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
| function flatten(a) { | |
| if (!Array.isArray(a)) { | |
| throw("Not an array: "+a); | |
| } | |
| let b = new Array(); | |
| append(a); | |
| return b; | |
| function append(x) { | |
| if (Array.isArray(x)) { | |
| for (let i = 0; i < x.length; i++) { | |
| append(x[i]); | |
| } | |
| } else { | |
| b.push(x); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment