I am trying to sum the elements inside a nested array. For example arraySum([1,[2,3],5,[[4]]]) should return 15
Everything seems ok except when I try to return array.pop() + arraySum(array) it goes into a weird infinite loop.
Everything goes well until code reaches that code. I tried to return the value of the array and I see no problems at all.
Can anyone tell me what's wrong with my code?
var arraySum = function(array) {
  
  if(array.length === 0){
      return 0
    }
  if(Array.isArray(array[array.length - 1])){
    var x = array[array.length - 1].reduce((accumulator, currentValue) => accumulator + currentValue);
    array.pop()
    array.push(x)
    return arraySum(array)
    }       
  return  array.pop() + arraySum(array)
};
console.log(arraySum([1,[2,3],5,[[4]]])) 
     
    