1

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.

Amine El were
  • 797
  • 2
  • 8
  • 21
  • 1
    Open your browser console. Type `Boolean(true)` and `Boolean(false)`. What are the results? – Taplar May 15 '20 at 19:00
  • 5
    Part of the confusion is because you're using `filter` incorrectly in your first solution. – John Montgomery May 15 '20 at 19:00
  • 2
    Here is the same topic discussed: https://stackoverflow.com/questions/29358815/using-boolean-as-the-argument-to-filter-in-javascript – Victoria Unizhona May 15 '20 at 19:02
  • I don't think the linked answer covers the confusion. Look at the unused `var trues`. That his filtered array is always empty, and he's using `filter` like `forEach`. This probably is a duplicate of something, but not of https://stackoverflow.com/questions/29358815/using-boolean-as-the-argument-to-filter-in-javascript – Aluan Haddad May 15 '20 at 19:05

2 Answers2

3

In this case Boolean is a function that returns true if the "thing in the array is truthy" and return false if the "thing in the array is falsy"

When you call filter every array element will have Boolean(the element) called on it and the result determines if the element makes it into the output array. Your array only has values true and false and Boolean() will return true for your true values and false for your false values. The result is the falses are filtered out.

Boolean() will also return true for non boolean values like 1 or "a", and false for non boolean non-values like null or undefined, so this approach would filter

[true, false, 1, "a", {}, null, undefined]

To

[true, 1, "a", {}]
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
1

Every value in the array will be passed to the Boolean() object wrapper, and it will cast (convert) values to true or false.

This is the same as:

const countTrue = r => r.filter(item => Boolean(item)).length

// Boolean(true) is true
// Boolean(false) is false
// Boolean(123) is true
// Boolean(0) is false

From MDN Docs:

The value passed as the first parameter is converted to a boolean value, if necessary. If the value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. All other values, including any object, an empty array ([]), or the string "false", create an object with an initial value of true.

Ivan V.
  • 7,593
  • 2
  • 36
  • 53