FCC Intermediate Algorithm Scripting: Steamroller
Flatten a nested array. You must account for varying levels of nesting.
function steamrollArray(arr) {
// I'm a steamroller, baby
let flatArr = [];
function recursionFunc(item) {
item.forEach(
function(entry) {
if (Array.isArray(entry) === true) {
recursionFunc(entry);
}
else flatArr.push(entry);
}
)
}
arr.forEach( function(item) {
if (!Array.isArray(item)) {
flatArr.push(item);
}
else {
recursionFunc(item);
}
})
return flatArr;
}
steamrollArray([1, [2], [3, [[4]]]]);
Comments
Post a Comment