Tell us what’s happening:
I have written the following code but it does not pass two tests out of four. The two tests which it did not pass are as follows:

Can someone please help me as to why it did not pass and where I went wrong??

Thanks !! Your code so far

function steamrollArray(arr) {
// I'm a steamroller, baby
let newArr = [];
const originalArr = [...arr];
const copyOfArr = [...arr];
let flattenArrFunc = function(copyOfArr){
for(let i=0;i<copyOfArr.length;i++){
// checks if the ith index is an array or a number
if(Array.isArray(copyOfArr[i]) && copyOfArr[i].length > 0){
// check if the array is empty or not
// if it is then skip the index
// if not then recursively call the function
flattenArrFunc(copyOfArr[i]);
}
else if(copyOfArr[i].length == 0){
delete copyOfArr[i];
}
else{
newArr.push(copyOfArr[i]);
}
}
if(newArr.length == originalArr.length){
return newArr;
}
}
return flattenArrFunc(arr);
}
steamrollArray([1, [2], [3, [[4]]]]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0.

Say I had an array [1, 2, , 5], where is an empty array, and ran the above code on it. What would happen? Is this result expected? I think you will find you don’t get the expected result.

Thanks for the reply!!
Yes, I did consider the scenario which you discussed ([1, 2, {}, 5]). Because of that, the following code makes sure that the empty array is deleted from the array of arrays (copyArr).

If the above code is not written, then the length of newArr returns to be 3 (since it only contains numbers) while that of the copyArr returns to be 4 (since it contains the empty array).

Yes, if the following condition is not met, then the function does not return anything.This won’t happen because I am removing empty arrays from the original array (copyArr) so that copyArr contains nothing but just numbers after all computations are performed.

I have attached the screenshot of the console. With reference to the screenshot, the left array refers to copyArr and right refers to newArr. It looks fine to me.

Can you please clarify what could be wrong??
Thanks!!

Thanks for the reply!! This is great since there is no need of changing the original array (using delete or splice) and not checking if newArr's length is equal to copyArr or not.

Also will the whole the process will have O(n) time complexity and O(m) (m is the depth of the deepest element of copyArr)space complexity??

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.