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

130
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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