Steamroller Coding Challenge

I tried to use the code below to get for the Intermediate Algorithm Scripting: Steamroller challenge. The thing is that it keeps saying that I failed it even though on the console the array shown is the expected answer.


function steamrollArray(arr) {
  // I'm a steamroller, baby

  let arrFlat = [];

  for(let i = 0; i < arr.length; i++){
    if(Array.isArray(arr[i])){
      if(arr[i].length === 0){
        continue;
      }
      arrFlat.push(steamrollArray(arr[i]));
    }else{
      arrFlat.push(arr[i]);
    }
  }
  
  //console.log(arrFlat);

  return [...arrFlat];
}

let d = steamrollArray([[["a"]], [["b"]]]);

console.log(d); 

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; WOW64) 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/

Does not show for me. In my console I am getting: d = [[[a]], [[b]]] when it should be d = [a, b]. :frowning:

You are doing recursion wrong. You are calling the upper function in recursion which resets the arrFlat[]

There are two ways to resolve this.

  1. Create a new function inside which keep the logic.

  2. Move the arrFlat[] outside the function at the top

Well it has been a while, just to update I was able to complete the challenge and learned a valuable lesson in not trusting the console on the site especially when it comes to arrays.