-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_True.js
More file actions
24 lines (21 loc) · 754 Bytes
/
count_True.js
File metadata and controls
24 lines (21 loc) · 754 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Create a function which returns the number of true values there are in an array.
// Examples:
// countTrue([true, false, false, true, false]) ➞ 2
// countTrue([false, false, false, false]) ➞ 0
// countTrue([]) ➞ 0
// Notes:
// Return 0 if given an empty array.
// All array items are of the type bool (true or false).
// function countTrue(arr) {
// return arr.filter(function (value) {
// return value === true;
// }).length;
// }
// console.log(countTrue([true, false, false, true, false]));
// console.log(countTrue([false, false, false, false]));
// console.log(countTrue([]));
// Method 2
function countTrue(arr) {
return arr.filter((n) => n == true).length;
}
console.log(countTrue([true, false, false, true, false]));