Question about the algorithm exercise called steamroller

Question about the algorithm exercise called steamroller
0

#1

Hello everyone I'm a newbie. And now I get stuck in this exercise

Here is my solution to it. I can get all correct answers to the parameters the system gives but the system judge me unable to pass the test.

var result = []; function count(arr){ var len = arr.length; for(var i=len-1; i>=0; i--){ if(Array.isArray(arr[i])){ count(arr[i]); }else{ result.push(arr[i]); } } return true; }

function steamrollArray(arr) {
count(arr);
result.reverse();
return result;
}

I hope someone can see this and help me ><. Thanks!


#2

Aah yes, as it happens, I had the EXACT same problem a few days ago. My solution was very similar to yours and also gave correct answers but could not pass the tests.

I suspect this might have something to do with the way global variables are handled in the test environment, but I really don’t know what could be causing it.

At any rate, all I can tell you is that I think your solution looks pretty decent. It answers the challenge. Unfortunately, I can’t help you with doing anything to actually make it pass the tests. Personally, I ended up re-doing all the code and finding another way to solve the challenge which subsequently passed the tests.


#3

Thanks. I decide to receive your advice and find other way to pass the challenge.


#4

You're right! The global variable goes something wrong. And I redefine it to be a local variable and it works. Thanks again!

Here is my code after fixed.

function count(arr, tem){ var len = arr.length; for(var i=len-1; i>=0; i--){ if(Array.isArray(arr[i])){ count(arr[i], tem); }else{ tem.push(arr[i]); } } return true; }

function steamrollArray(arr) {
var result = [];
count(arr, result);
result.reverse();
return result;
}


#5

Awesome! Keep it up!