Skip to content

Instantly share code, notes, and snippets.

@eday69
Last active June 15, 2018 17:53
Show Gist options
  • Save eday69/22fa6d6e3f65396beeb510a24925786f to your computer and use it in GitHub Desktop.
Save eday69/22fa6d6e3f65396beeb510a24925786f to your computer and use it in GitHub Desktop.
freeCodeCamp Intermediate Algorithm Scripting: Steamroller
// 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