DEV Community

Oscar Hernandez
Oscar Hernandez

Posted on

Intermediate Algorithm Scripting: Steamroller FCC

/Intermediate Algorithm Scripting: Steamroller
Flatten a nested array. You must account for varying levels of nesting.
/
function steamrollArray(arr) {
var x = arr.reduce((acc, val) => Array.isArray(val) ? acc.concat(steamrollArray(val)) : acc.concat(val), []);
console.log(x)
return x
}
steamrollArray([1, [2], [3, [[4]]]]);
steamrollArray([[["a"]], [["b"]]]);
steamrollArray([1, [], [3, [[4]]]]);
steamrollArray([1, {}, [3, [[4]]]])
/https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller/
/https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Array/flat/

Top comments (0)