I have to create a function that receives an array in which every element is a number, or it can be another array with numbers inside, for example : const array = [1, [2, [3,4]], [5,6], 7]; The function should count all the values and return it countArray(array); --> should return 28 because (1 + 2 + 3 + 4 + 5 + 6 + 7)
I tried this
const array = [1, [2, [3,4]], [5,6], 7];
var countArray = function(array) {
let sum = 0
for (let i = 0; i < array.length; i++) {
if (Array.isArray(array[i])) {
countArray(array[i])
}
sum = sum + array[i]
}
return sum
}
console.log(countArray(array));
but this does not work, anybody knows why?