Need Help With SteamRoller Algorithm


#1

I’m doing intermediate algorithm challenge and kind of got stuck with this challenege. problem is that actually I do get correct answer for every test array but none is accepted as correct answer. I see only difference is space between array elements but is that even thing? how should I put space around numbers? please check my code and help to understand what am I doing wrong.

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

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

#2

Your code is correct but is not accepted because of the global variable. Revise your code so that there are no globals.


#3

you sure about that? I do know global variable is not a good practice but solution with it looked so simple I just could not think of anything else even though I tried for 2 hours :slight_smile: so I decided to complete it like that and then go see hint and proper solution :slight_smile: if its all there is than I guess I will have to do it proper way myself.


#4

It’s a pretty common problem encountered by people working on the challenges. Luckily it’s easy to remove your global variable without changing much of the code.