Find the number of true in the array.

Create a function trueCount which returns the number of true values in the array.

Examples

trueCount([false, true, false, true, false]) ➞ 2

trueCount([true, true, true, true]) ➞ 4

trueCount([]) ➞ 0

Solution:

const count_true = r => r.filter(Boolean).length

or

function trueCount(arr) {
const trueCount = arr.reduce((total, item)=>total+Number(item), 0)
return trueCount;
}

Sunil Yadav

About Sunil Yadav

Hi, This is Sunil Yadav, I am full stack dev. I have a passion for learning and sharing my knowledge with others as publicly as possible.

View all posts by Sunil Yadav →

Leave a Reply

Your email address will not be published. Required fields are marked *