Array flattening challenge

Tell us what’s happening:
Describe your issue in detail here.

I got to this point of the code. i kinda have an idea where im headed. i know i need to set up some recursion. but im not sure of the best way to set it up. some advice on getting to the right direction would be helpful.

  **Your code so far**

function steamrollArray(arr) {
let steamroll=[];

for (let i = 0; i < arr.length; i++){

        if (Array.isArray(arr[i])){
         steamroll.push(arr[i])
        }
    
}
return arr;
}

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/92.0.4515.131 Safari/537.36

Challenge: Steamroller

Link to the challenge:

I would recommend starting with how to begin any recursive function. Think of the base case (when the algorithm will stop), then work from there. Ask yourself: if this condition is met return the result... otherwise recurse

This is where youd want to have your recursive call. And don’t push it to your holder array if it is an array. If it’s not an array then you will push it to your holder

Are you mutating the array? Return your holder array i.e. steamroll

oh i wasnt intending on returning that. i just havent changed the new return yet.

yeah thanks alot, i thought thats where the recursion took place from the hint the last poster gave. i just need to figure out how to set up the recursion.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.