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

Popular posts from this blog

Code Wars: longest_palindrome (6 kyu)

Code Wars: Data Reverse (6 kyu)

Code Wars: Replace With Alphabet Position