4.13 Exercise: Counting Truthy Value

I want you to create a function called countTruthy that takes an array and returns a number of true elements in that array. Now, are these values true or false? Let me quickly refresh your memory. Sometimes when we have an if statement, we can pass a boolean true or false as a condition here, for example, we may have a constant called Active, we set it to true, and when we use that constant as a condition here, we get this Hello message on the console. Because it's a true Boolean. But sometimes, what we have in our if statement is not necessarily a Boolean value, it could be a string, it could be a number, So the JavaScript engine tries to convert that value into something that looks like a Boolean but isn't. Here's an example, let's say I have a constant, called name, that I put into a string that we'll call "Toto". Now, if I pass the name here, the name is obviously a string, It's not a Boolean, so the JavaScript engine tries to interpret it as true or false, so when we run this code, you can see that we still see the good morning message, because we're dealing with a string. That's what we call a true value. It's not the boolean true, it's a truthy. On the other hand, In review, if we had an empty string, the JavaScript engine would register it as false. Again, this is not the boolean type false. This is a falsey. Now, when we execute this code we don't see the Hello message anymore. This is for true and false values. So now, your job is to create a function called CountTruthy that gets an array and returns the number of Truthy values in that array. So here's an example, if I declare this array and set it to 1, 2 and 3, So here we have 3 truthy values. We call countTruthy, pass the array and immediately send the result to the console. log. Save the changes and we get 3 here. Now, if we add 0 here, because 0 is a falsey value, we still get 3. If we add a null, undefined and an empty string, we always get 3. If I delete one of these numbers, say 1, we get 2. So go ahead, do this exercise. And when you're done, come back and keep looking at the correction. So, to count the number of Truthy values, I'm going to delar a variable called count and initialize it to 0. Now we need a for loop to iterate through this array and for each element we have to check if this element is a truthy or not. If it is true, you will implement the count. So we do a for let value of array, now if this value is truthy, we just increment the number and finally we return that count. So notice here that I'm not comparing this value with all these falsey values. In other words, I'm not writing code like this so if the value is not false, or if the value is not defined, this is not an efficient way to write this code, we don't want to have 5 conditions here for every false value, just pass that value here. Now this value can be boolean or not, if it's not a boolean, the JavaScript engine tries to interpret it as truthy or falsey. So if the result is truthy, then the name will be incremented. That's it for this little demonstration on truthy and falsey values, we'll meet again for the next demonstration.