Tell us what’s happening:
Describe your issue in detail here.
I’ve found recursive to be a bit challenging to conceptualize but I found it necessary to use it in this algorithm after trial and error with nested loops.
The code I have is correct but I don’t understand how this code does not infinitely loop if there is an empty array, or if the last element in the entire array is an empty Array. Like I said it works even with some ugly multi dimensional arrays I’ve tested.
Thanks in advance for your help as I will need it taking on recursion.
**Your code so far**
function steamrollArray(arr) {
let newArr = [];
function checkArr(arr) {
for (let ele of arr) {
if (!Array.isArray(ele)) {
newArr.push(ele);
} else checkArr(ele);
}
}
checkArr(arr);
return newArr;
}
steamrollArray([1, [2], [3, [[4, [[]]]]]]);
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
Challenge: Steamroller
Link to the challenge: