Question: 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

```js
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?

Answer: 
To spare you the effort of recursion here's a simple trick.

Ordinarily, on its own, [`flat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) will only flatten a series of nested arrays one level deep. If you supply `Infinity` as an argument you can flatten nested arrays `n`-level deep, and then you can [`reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) over those elements to get the sum.

```js
const array = [1, [2, [3,4]], [5,6, [3, [23, [1, 2, [12]]]]], 7];

function countArray(arr) {
  return arr
    .flat(Infinity)
    .reduce((acc, c) => acc + c, 0);
}

console.log(countArray(array));
```
Changing as little as possible about your code, you need add either the `countArray` of the item (if the item is an array) **or** the item itself (if it's not an array) to the sum.

```js
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])) {
      sum = sum + countArray(array[i])
    } else {
      sum = sum + array[i]
    }
  }
  return sum
}

console.log(countArray(array));
```