Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

301
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda