Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

302
Visualizações
Javascript function that counts how many numbers are in the array (the array can contain other arrays)

How can I write a function that, received an array a already given from the program that can contain numbers, boolean and even other arrays, counts how many numbers are present in a?

Here's my code that doesn't work (it gives everytime 0 whatever the array contains):

function test(a) {
    var sum = 0
    for (i=0; i<a.length; i++){
       if (typeof a[i]==Number)
           sum++                    
    }
    return sum 
}

Thanks.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

you can try this:

const array = ["Hi", true, 10, "Car", 20, 50, 1500, false];
const flatedArray = array.flat(Infinity);
const count = flatedArray.filter(x => typeof x == 'number').length;
console.log(count) // 4

or change your code:

function test(a) {
    const flatedArray = a.flat(Infinity);
    let sum = 0
    for (i = 0; i < flatedArray.length; i++) {
        if (typeof a[i] == 'number') {
            sum++
        }
    }
    return sum
}

const array = ["Hi", true, 10, "Car", 20, 50, 1500, false];
console.log(test(array)) // 4

or change your function:

function test(a) {
    const flat = a.flat(Infinity);
    return a.filter(x => typeof x == 'number').length;
}

const array = ["Hi", true, 10, "Car", 20, 50, 1500, false];
console.log(test(array)) // 4
about 4 years ago · Juan Pablo Isaza Relatório

0

You have some issues:

  • typeof <operand> returns a string and not an object, you should be checking against a string typeof a[i] === "number".
  • You should be using var/let before i in your for loop so that i doesn't become a global variable and instead remains scoped to your function (if you use var) or your for loop (if you use let). Having variables accidentally added to the global scope can easily lead to bugs for code that runs after you call your test() function.

As you want to count the frequency of numbers in both your array and nested arrays, you can use recursion. The idea is to call your test() function again inside of test() when you encounter an array:

function test(a) {
  let sum = 0;
  for (let i = 0; i < a.length; i++) {
    if (typeof a[i] === "number")
      sum++
    else if(Array.isArray(a[i]))
      sum += test(a[i]); // sum the numbers within the `a[i]` array
  }
  return sum;
}

console.log(test([1,4, [2, 3, true], ["foo",0], 5, [123, [3.14, {a: 1}]]])); // 8

The other option is to get JS to handle the recursion for you, which can be done with the help of the .flat() method. By using .flat(Infinity) you can first flatten your array (ie: remove the inner arrays of any level), and then loop over it:

function test(a) {
  let sum = 0;
  let flat = a.flat(Infinity);
  for (let i = 0; i < flat.length; i++) {
    if (typeof flat[i] === "number")
      sum++;
  }
  return sum;
}

console.log(test([1,4, [2, 3, true], ["foo",0], 5, [123, [3.14, {a: 1}]]])); // 8

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda