I am trying to smooth an array with randomly nested arrays inside. I'm not sure why the function I wrote ends in an infinite loop:
let array = [1, 2, [3]]
var final_array = []
function flattener(array){
for(i = 0; i < array.length; i++){
if(array[i] instanceof Array){
flattener(array[i])
}else{
final_array.push(array[i])
}
}
}
flattener(array)
I think it MUST be:
When I am in the for loop for [3], it goes into the if statement, flattenergets called again, it resolves, and then I exit the if statement.
Instead, the if statement continues to trigger the check [3]indefinitely, and I'm not sure why this is happening.
source
share