Last active
June 15, 2018 17:53
-
-
Save eday69/22fa6d6e3f65396beeb510a24925786f to your computer and use it in GitHub Desktop.
freeCodeCamp Intermediate Algorithm Scripting: Steamroller
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
// Flatten a nested array. You must account for varying levels of nesting. | |
function steamrollArray(arr) { | |
// I'm a steamroller, baby | |
return arr.reduce((acc, val) => Array.isArray(val) ? acc.concat(steamrollArray(val)) : acc.concat(val), []); | |
} | |
steamrollArray([1, [2], [3, [[4]]]]); // [1, 2, 3, 4] | |
steamrollArray([[["a"]], [["b"]]]); // ["a", "b"]. | |
steamrollArray([1, [], [3, [[4]]]]); // [1, 3, 4]. | |
steamrollArray([1, {}, [3, [[4]]]]); // [1, {}, 3, 4]. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment