Created
November 26, 2015 03:45
-
-
Save minsooshin/34be42057c69bced5b60 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/minsooshin 's solution for Bonfire: 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
// 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