Skip to content

Instantly share code, notes, and snippets.

@minsooshin
Created November 26, 2015 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minsooshin/34be42057c69bced5b60 to your computer and use it in GitHub Desktop.
Save minsooshin/34be42057c69bced5b60 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/minsooshin 's solution for Bonfire: Steamroller
// Bonfire: Steamroller
// Author: @minsooshin
// Challenge: http://www.freecodecamp.com/challenges/bonfire-steamroller
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function steamroller(arr) {
// I'm a steamroller, baby
function flatten(array, newArr) {
if (newArr === undefined) newArr = [];
if (Array.isArray(array)) {
array.forEach(function(index) {
flatten(index, newArr);
});
} else {
newArr.push(array);
}
return newArr;
}
return flatten(arr);
}
steamroller([[["a"]], [["b"]]]); //=> ["a", "b"]
steamroller([1, [2], [3, [[4]]]]); //=> [1, 2, 3, 4]
steamroller([1, [], [3, [[4]]]]); //=> [1, 3, 4]
steamroller([1, {}, [3, [[4]]]]); //=> [1, {}, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment