Steam roller evaluation issues

I can make my code pass all the tests and give me the right answers in the console but i can’t seem to get the tests to pass when I run it

  **Your code so far**


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

for (let i = 0; i < arr.length; i++){
  if(Array.isArray(arr[i])){
    ret = steamrollArray(arr[i]);
  } else {
    ret.push(arr[i]);
  }
  }
return ret;

}

console.log(steamrollArray([[["a"]], [["b"]]]));
  **Your browser information:**

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

Challenge: Steamroller

Link to the challenge:

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like

I made it a local variable before and it was giving me an error. I realized that my statement of ret = steamrollArray(arr[i]); is only checking the singular element i and closing the loop,

function steamrollArray(arr) {
let ret = [];
  for (let i = 0; i < arr.length; i++){
    if(Array.isArray(arr[i])){
      ret.push(...steamrollArray(arr[i]));
    } else {
      ret.push(arr[i]);
    }
    }
  return ret;
  }

I changed it to this here so that it unpacks everything in arr[i] . worked wonderfully thanks for the help

1 Like