Help with "steamroller" challenge

My code so far
the challenge: https://www.freecodecamp.com/challenges/steamroller

function steamrollArray(arr) {
  // I'm a steamroller, baby
  var leng = arr.length;
  for (var i = 0; i < leng; i++){
    if (Array.isArray(arr[i])){
      for(var j = 0;j < arr[i].length;j++){
      arr.push(arr[i][j]);
  
      }
    }
  }
 return arr;
}

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

Ok, so in this, I push the values inside arrays into the array arr , and my idea was to push all values inside arrays until no arrays remainded, by calling the function again and again, but to this to work, I need to remove the arrays after pushing it’s contents, and I can’t find out exactly how to do it.
This is probably the worst method of tackling this challenge, but in my head this can work, and it’s the only way I can think of solving it.

Check out the splice function. It will “almost” do what you want.

This brings back memories, this was my first problem in fcc way back. The simplest solution that I came up with uses the function reduce of Array, and a concept called recursion. I would look into how reduce works in mdn and also would look into what recursion is. mdn also has a definition of it but it’s very basic.

Hopefully with those two things you can arrive at a solution.

1 Like