Tell us what’s happening:
Hello, I am struggling with this one. Initially I was struggling with having undefined values being pushed into my return array, but I realized it was because it had no return statement.
I updated that, and it started pushing actual values. However now I am having the issue of having the return statement in the for loop. Obviously, that will terminate after the first loop, so that won’t work. But I cannot for the life of me figure out how to hold the values from the for loop and push them afterwards.
I have tried adding an array outside of the for loop and pushing the values to that, but I run into the same issue of it pushing undefined values.
unwrapIfArr was initially made because of running into this same problem with steamrollArray. I thought creating a separate sub-method would fix the issue, but apparently I was wrong, lol.
It feels like there’s something simple I’m missing, but I can’t figure it out. Any help would be appreciated. Thank you!
Your code so far
function steamrollArray(nestArr) {
function unwrapIfArr(element) {
if (Array.isArray(element)) {
console.log(`${element} is an array. Starting for loop`);
for (let item of element) {
console.log(`Current item:`);
console.log(item);
return unwrapIfArr(item);
}
}
else {
console.log(`${element} is not an array. Returning ${element}`);
return element;
}
}
let result = [];
for (let item of nestArr) {
let toPush = unwrapIfArr(item);
console.log(`Item to push:`);
console.log(toPush);
result.push(toPush);
console.log(`Pushed to result. New result:`);
console.log(result);
}
console.log(`For loop completed. Returning result:`);
console.log(result);
return result;
}
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/144.0.0.0 Safari/537.36
Challenge Information:
Create a Deep Flattening Tool - Create a Deep Flattening Tool