Function does not pass two tests

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:

1. steamrollArray([1, [2], [3, [[4]]]])` should return `[1, 2, 3, 4] .
2. steamrollArray([1, {}, [3, [[4]]]])` should return `[1, {}, 3, 4] .

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.

Challenge: Steamroller

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller

First of all, welcome to the FCC community!

There are a few problems I see with your code.

Consider these lines:

    else if(copyOfArr[i].length == 0){
      delete copyOfArr[i];
    }

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.

Secondly, these lines:

  if(newArr.length == originalArr.length){
    return newArr;
  }

What happens if this condition is not met? What does the function return? Is that value defined or undefined?

Let me know if you still don’t understand - happy to help!

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).

else if(copyOfArr[i].length == 0){
      delete copyOfArr[i];
    }

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.

 if(newArr.length == originalArr.length){
    return newArr;
  }

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!!

Regarding the first point, delete isn’t the correct method to use in this case. See here: https://stackoverflow.com/questions/500606/deleting-array-elements-in-javascript-delete-vs-splice

You should use splice. But do you really need to delete anything? See if there is a way you can implement this without deleting from the array.

Regarding the second point, you are pushing elements onto newArr in the “else” condition, so the lengths will be different:

  else{
      newArr.push(copyOfArr[i]);
    }

so that condition will be false whenever copyOfArr[i] is not a nested structure, causing your function to return undefined.

1 Like

I have fixed the bugs in your code:

<redacted>

Let me know if you have any additional questions!

1 Like

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??

Yes. To be clear, the time complexity should be O(n) where n is the number of integers in the input array.

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.

Thank you for understanding.

1 Like