I tried to work the JavaScript challenge of - flattening nested arrays - using recursion but I keep getting “[1]” as the return value, please help me find what I am (obviously) not getting right
Here’s my code:
function steamrollArray(arr) {
const flattened = [];
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
flattened.concat(steamrollArray(arr[i]));
} else {
flattened.push(arr[i]);
}
return flattened;
}
}
console.log(steamrollArray([1, [2], [3, [[4]]]]));
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36
.
Challenge: Steamroller
Link to the challenge: