Steamroller InterMediateAlgorithm

Steamroller InterMediateAlgorithm
0.0 0

#1
var result= [];
function steamrollArray(arr) {
   for(var i = 0; i < arr.length; i++){
       if(!Array.isArray(arr[i]))  result.push(arr[i]);
       else steamrollArray(arr[i]);
   }
  return result;
}
steamrollArray([1, {}, [3, [[4]]]]);

It works all cases, but why not pass?


#2

why are you using a global variable?

add another
steamrollArray([1, {}, [3, [[4]]]]);
to your code and you’ll see what’s wrong;


Steamroller test bug
#3

You are using a global variable.

Global variables persist after function calls have completed, so we have to be very careful about modifying globals in functions. Your code relies on result being an empty array when the function is called, but after steamrollArray has been executed it is no longer an empty array. This means that your function will only work once.