Global Var issue with Steamroller

Global Var issue with Steamroller
0

#1

I’m aware the the issue seems to have something to do with my Global var but I’m not sure why my resetting the global array in the steamroller function doesn’t pass.

var answer = [];

function flatten(source){
     source.forEach((val) => Array.isArray(val)? flatten(val) : answer.push(val)); 
     return answer;
}

function steamrollArray(arr) {
  answer = [];
  return flatten(arr);
}


#2

Every test succeeds on the freecodecamp page, is it not working on your side ? In order to avoid using global variable, I would do this :

function steamrollArray(arr) {
  var answer = [];
  
  function flatten(source){
     source.forEach((val) => Array.isArray(val) ? flatten(val) : answer.push(val)); 
     return answer;
  }
  
  return flatten(arr);
}

#3

Yeah not working for me but I’ll try it your way.


#4

Thanks - that did the trick.