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

224
Views
How do I count the number of truth values in array?

I am struggling with some logic but I think I am close. Is there any way to get the number of truth values from an array of booleans?

  const checkedState = [true, false, true]

  function handleCourseProgress() {
    //for each value that is true in the array...
    checkedState.forEach((value) => {
      //if the value is true...
      if (value === true) {
        //count and total them up here....
        setCourseProgress(//return the count//);
      }
    });
  }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The easiest way is with reduce. Here's the example.

const checkedState = [true, false, true]

const answer = checkedState.reduce((acc, val) => val ? acc + val : acc);

console.log(answer)

about 4 years ago · Juan Pablo Isaza Report

0

filter out the elements that are true, and return the length of that array.

const one = [false, true, true];
const two = [true, true, true];
const three = [false, false, false, false];

function trueElements(arr) {
  return arr.filter(b => b).length;
}

console.log(trueElements(one));
console.log(trueElements(two))
console.log(trueElements(three))

about 4 years ago · Juan Pablo Isaza Report

0

const checkedState = [false, true, false, true, true]

const count = checkedState.filter((value) => value).length
// Cleaner way
const anotherCount = checkedState.filter(Boolean).length

console.log(count)
console.log(anotherCount)

Basically filtering the array and looking for the truthy values and checking the length of the array will do the trick and after that you can call the setCourseProgress(count) with the right count value.

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!