I'm trying to practice some javascript so I did some coding challenges everything went well except that I run into a problem with a challenge that says that I should create a function which returns the number of true values there are in an array.
example:
countTrue([true, false, false, true, false]) ➞ 2
I solved the challenge with the filter() method and everything is Ok and here's what I did to solve it
function countTrue(arr) {
var s=0;
arr.filter((i)=>{
if (i=== true) {s++}
})
return s
}
but there's a shorter solution which is this:
const countTrue = r => r.filter(Boolean).length
I understood everything except that Boolean
parameter which was passed to the filter method, I did a research about the filter() but I didn't find anything useful.
please If somebody knows the answer, enlighten me and thank you in advance.