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

132
Vistas
How to log how many possibilities fulfil the if statement javascript

const grades = [9, 8, 5, 7, 7, 4, 9, 8, 8, 3, 6, 8, 5, 6];
for (let i = 0; i < grades.length; i++) {
  if (grades[i] >= 8) {
    console.log(grades[i])
  }
}

I'm trying to log how many items from the array fulfil the condition. the output I'm looking for is : 6 (because 6 of the numbers are equal or greater than 8)

tried

let count = 0; for (let i = 0; i < grades.length; i++) {

if (grades[i]>= 8){ count++

console.log(count)

}

}

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

function countGreaterThan8(grades){
    // initialize the counter
    let counter = 0;
    for (let i = 0; i < grades.length; i++) {

      // if the condition satisfied counter will be incremented 1
      if (grades[i] >= 8) {
        counter++;
      }
    }
    return counter;
}

const grades = [9, 8, 5, 7, 7, 4, 9, 8, 8, 3, 6, 8, 5, 6];
console.log(countGreaterThan8(grades)); // 6
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can call Array.filter to create a new array containing only items that fulfill the condition. You can then make use of the length of the array however you want. Like this

const grades = [9, 8, 5, 7, 7, 4, 9, 8, 8, 3, 6, 8, 5, 6];
const gradesThatPassCondition = grades.filter(grade => grade > 6);
console.log(gradesThatPassCondition.length);
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