Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!