Is there a better solution than mine for Steamroller?

Is there a better solution than mine for Steamroller?
0

#1

This is the purpose of the algorithm:

Flatten a nested array. You must account for varying levels of nesting.

Right now my solution works, but I want it to be more efficient.

For example. Right now it is limited to the amount of depth I have put in my code.

How can I check how many arrays there are inside of each of the arrays without writing so much repetitive code?

Any ideas will be much appreciated!

Thanks.

Your code so far


function steamrollArray(arr) {
  let newArr = [];
  for(let i = 0; i < arr.length; i++){
    if(Array.isArray(arr[i])){
      for(let j = 0; j < arr[i].length; j++){
        if(Array.isArray(arr[i][j])){
          for(let y = 0; y < arr[i][j].length; y++){
            if(Array.isArray(arr[i][j][y])){
              for(let x = 0; x < arr[i][j][y].length; x++){
                if(!Array.isArray(arr[i][j][y][x])){
                  newArr.push(arr[i][j][y][x]);
                }
              }
            } else {
              newArr.push(arr[i][j][y]);
            }
          }
        } else {
          newArr.push(arr[i][j]);
        }
      }
    } else {
      newArr.push(arr[i]);
    }
  } 
  return newArr;
  console.log(newArr); 
}

steamrollArray([1, 2, [3], [1, 2]]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

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


#2

Yes there is.

Your solution currently works because you have 4 levels of nesting. But what if you would have to execute your function on the following array:

[[[[[[[[[[[[[[[[[[[[[4, 5]]]]]]]]]]]]]]]]]]]], [[[[[[[[8]]]]]]]]]

You better not to go that deep! Instead, you must use something we call recursion.

Based on what you’ve done, try to figure out a pattern and use arrays functions to build a general solution.

Hint: you will probably have to use reduce.


#3

Thanks. I have already checked the original solutions. But I will try to figure it out with reduce.


#4
  • The code within you function is equivalent to return arr.flat(3) (ie you could replace it all with just that and the tests would still pass fine).
  • But as pointed out, that isn’t a general solution as it only goes to a depth of 3.
  • if you don’t want to use recursion (and recursion is the easiest way to solve this), you can use a stack (which should start as a copy of arr, but you could just use arr itself).
    1. You keep iterating while there are values in the stack (while (stack.length) {).
    2. Every iteration, pop the stack.
    3. If the value you pop off is not an array, push that value into newArr.
    4. If it is an array, push the values back onto the stack (eg stack is [1,2,[3, [4]]]. Pop gives you [3,[4]]. It’s an array, so push 3 and [4], in that order, back onto the stack (using push(...[3,[4]]) is easiest), so stack is now [1,2,3,[4]]).
    5. Then return newArr at the end as you do there, but make sure to reverse it because it will be backwards (or use unshift instead of push on step 3, but reverse will be more performant)