My code doesn't pass the tests even though the answers are correct

Hello everyone! I’m currently working on the “Intermediate Algorithm Scripting: Steamroller” lesson from the JS course. By verifying the tests manually, it seems to me that everything is working fine, but apparently FCC doesn’t agree with me, and I don’t know why. Here is my code:

let newArr = [ ];

function steamrollArray(arr) {

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

  return newArr;

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

Any idea please? Thank you very much!

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.


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

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

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thank you very much! But I don’t really understand, when I was doing a console.log on the function, it showed the correct results. What happens in the background?

try adding two different function calls one after the other and check what’s the output of the second one

Oh I see now! Thanks a lot, I really appreciate!