Steamroller i'm getting expected values bad don't work

Steamroller i'm getting expected values bad don't work
0.0 0

#1

Tell us what’s happening:
i can’t see where did i make a mistake.
Actually it’s should work because i’m getting expected values
[1, {}, [3, [[4]]]] returned -> [1, {}, 3, 4]

Your code so far

var re=[];

function test1(arr){
    if(Array.isArray(arr) & arr.length > 1 ){
      test2(arr);
    }else if(Array.isArray(arr[0])){
      test1(arr[0]);
    }else{
      if(arr[0] != null )re.push(arr[0]);
    }
}

function test2(arr){
  for(var i=0;i<arr.length;++i){
    if(Array.isArray(arr[i])){
      test1(arr[i]);
    }else{
     if(arr[i] != null )re.push(arr[i]);
    }
  }
}

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

steamrollArray(   [1, {}, [3, [[4]]]]  );

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:


#2

I think the issue might be that you have an object {} as your second array element, the challenge only has arrays.


#3

I guess there is a another problem ???


#4

my bad, I missed the last test. try console.log(arr) at the end of your solution (just before return arr) and you’ll see the problem :slight_smile:


#5

You declared re globally, so since the FCC tests run consecutively, re retains the value it had at the end of the the previous test. You will need to move the re declaration to be inside your steamrollArray function and then when ever you call test1 or test2, you will need to pass it as a second argument, so those two functions will have access to it.

Another approach, is to declared re inside the steamrollArray function and move your existing test1, and test2 function definitions inside the steamrollArray function. That way, they will have access to re from inside of steamrollArray. This approach would not require you to pass in re to the test1 and test2 functions.