Skip to content

Instantly share code, notes, and snippets.

@clive-bunting
Created December 5, 2015 15:32
Show Gist options
  • Select an option

  • Save clive-bunting/fa0b366cc2290b79ece4 to your computer and use it in GitHub Desktop.

Select an option

Save clive-bunting/fa0b366cc2290b79ece4 to your computer and use it in GitHub Desktop.
Bonfire: Steamroller
// Bonfire: Steamroller
// Author: @clive-bunting
// Challenge: http://www.freecodecamp.com/challenges/bonfire-steamroller#
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function steamroller(arr) {
var flat = [];
for (var i =0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
var flattened = steamroller(arr[i]);
flattened.forEach(function(a){flat.push(a);});
}
else {
flat.push(arr[i]);
}
}
return flat;
}
steamroller([1, [2], [3, [[4]]]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment