Skip to content

Instantly share code, notes, and snippets.

@Narshe1412
Created December 17, 2015 19:41
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 Narshe1412/75734adfdbd4c75bd6cc to your computer and use it in GitHub Desktop.
Save Narshe1412/75734adfdbd4c75bd6cc to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/narshe1412 's solution for Bonfire:Steamroller
function steamroller(arr) {
// I'm a steamroller, baby
return arr.reduce(function (flat, toFlatten) {
return flat.concat(Array.isArray(toFlatten) ? steamroller(toFlatten) : toFlatten); //ternary
}, []);
/*
Uses the same concat flatten example on the MDN, but with recursion to further flatten
ex:
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {
return a.concat(b);
}, []);
In this case b is checked if is still an array, then steamroller is called again if it is.
Otherwise it's just returned
*/
}