'Count the number of true members in an array of boolean values

New to javascript and I'm having trouble counting the number of trues in an array of boolean values. I'm trying to use the reduce() function. Can someone tell me what I'm doing wrong?

   //trying to count the number of true in an array
    myCount = [false,false,true,false,true].reduce(function(a,b){
      return b?a++:a;
    },0);
    alert("myCount ="+ myCount);  // this is always 0


Solution 1:[1]

Seems like your problem is solved already, but there are plenty of easier methods to do it.

Excellent one:

.filter(Boolean); // will keep every truthy value in an array

const arr = [true, false, true, false, true];
const count = arr.filter(Boolean).length;

console.log(count);

Good one:

const arr = [true, false, true, false, true];
const count = arr.filter((value) => value).length;

console.log(count);

Average alternative:

let myCounter = 0;

[true, false, true, false, true].forEach(v => v ? myCounter++ : v);

console.log(myCounter);

Solution 2:[2]

You're returning a++ when the value is true, which will always be zero. Post-increment happens after the value is retrieved. So on the first iteration, a is 0, and the value of a++ is also 0, even though a is incremented. Because a and b are parameters of the callback, it's a fresh a on each call.

Instead:

myCount = [true, false, true, false, true].reduce(function(a, b) {
  return b ? a + 1 : a;
});

console.log(myCount);

Solution 3:[3]

Well there are multiple ways you can do it.

  1. Boolean with filter()

     const arrayCount = [false,false,true,false,true].filter(Boolean).length;
     console.log(arrayCount);
    
  2. filter

     const arrayCount = [false,false,true,false,true].filter(item => item).length;
     console.log(arrayCount);
    
  3. reduce()

    const arrayCount = [false,false,true,false,true].reduce((acc, current) => acc + current, 0);
    console.log(arrayCount);
    
  4. Using function

    function arrayCount(arr) {
     let result = [];
     for(let i = 0; i < arr.length; i++) {
         if (arr[i] === true) {
             result.push(arr[i]);
         }
     }
     return result.length;
    }
    
    console.log(arrayCount([false,false,true,false,true]))
    
  5. Using for..of

      function arrayCount(arr) {
          let count = 0;
          for(let element of arr) if(element===true) count++;
          return count;
     }
    
    console.log(arrayCount([false,false,true,false,true]))
    

Solution 4:[4]

You should use ++a instead a++ because you have to change the value of a suddenly. a variable will be incremented after its value is returned.

 myCount = [false,false,true,false,true].reduce(function(a,b){
      return b? ++a:a;
    },0);
alert("myCount ="+ myCount); 

Solution 5:[5]

I am not really sure about this, but a and b aren't numbers.

You shoud do something like :

//trying to count the number of true in an array
myCount = [false,false,true,false,true].reduce(function(a,b){
cpt = 0;
if(a) cpt++;
if(b) cpt++;
return cpt;
},0);
alert("myCount ="+ myCount);  // this is always 0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Teocci
Solution 3 Melongro
Solution 4
Solution 5 Youri