I’m working on the steamroller challenge. I actually got this code (or something very similar? It’s possible I may have changed it unwillingly) to work, but because the function I wrote takes two arguments it didn’t pass the tests. So I decided to simply remove it and put it into its own function, and then I can pass it whatever arguments I want.
// takes two arguments. First is the array to
// flatten. arr2 is an empty array for storing
// the results.
function smasher(arr1,arr2){
// step through every element in arr1
for(i = 0; i < arr1.length; i++){
// if it is NOT an array, push it into arr2 as it is.
if(Array.isArray(arr1[i]) === false){
arr2.push(arr1[i]);
// if it IS an array, run it through this function again
// then loop through the array it returns (x) and
// push all those values to arr2
} else {
var x = smasher(arr1[i],[]);
for(j = 0; j < x.length; j++){
arr2.push(x[j]);
}
}
}
return arr2;
}
However, now no matter what I do, running this locks up the FCC page. What’s worse, it even locks up if I feed it a simple, non-nested array such as [1,2,3,4,5], so it should never even branch into the “else” statement and hit the recursive code.
I’ve been thinking about the logic here all day, and I simply cannot see where it is failing.