How Much is True?
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
orfalse
).
Answer (1)
const countTrue = r => r.filter(Boolean).length
Answer (2)
function countTrue(arr) {
return arr.filter(x=>x==true).length
}
Answer (3)
function countTrue(arr){
var count = 0;
for(var i = 0; i < arr.length; i++){
if(arr[i] === true){
count++;
}
}
return count;
}
Answer (4)
const countTrue = (arr) => arr.filter(Boolean).length;
Answer (5)
countTrue=a=>a.filter(b=>b).length
Answer (6)
function countTrue(arr) {
let trueCounter = 0;
arr.forEach((bool) => {
if (bool) {
trueCounter++;
}
});
return trueCounter;
}
Answer (7)
function countTrue(arr) {
var reducer = (acc, curVal) => acc + curVal;
return arr.reduce(reducer, 0);
}
Answer (8)
function countTrue(arr) {
return arr.filter(x => x === true).length
}
Answer (9)
function countTrue(arr) {
return arr.reduce((p,v)=>p+v,0);
}
countTrue=a=>a.filter(b=>b).length
Answer (10)
function countTrue(arr) {
let count=0
for(let element of arr)
if(element===true)
count++
return count
}
Answer (11)
function countTrue(arr) {
var n=0, l=arr.length;
for(var i=0;i<l;i++) if(arr.shift()) n++;
return n;
}
Answer (12)
function countTrue(arr) {
return arr.filter(Boolean).length
}
Answer (13)
function countTrue(arr) {
let count = 0;
for(let x of arr){
if(x){
count++;
}
}
return count;
}
Answer (14)
function countTrue(arr) {
let result = new Array();
for(let i = 0; i < arr.length; i++) {
if (arr[i] === true) {
result.push(arr[i]);
}
}
return result.length;
}
Answer (15)
function countTrue(arr) {
trueCount = 0
for(element of arr ){
if (element === true){
trueCount+=1
}
else continue
}
return trueCount
}
Answer (16)
function countTrue(arr) {
var count = 0;
arr.filter(function(elem){
if(elem == true)
count+=1
})
return count
}
Answer (17)
countTrue=a=>a.filter(c=>c).length
Answer (18)
function countTrue(arr) {
var count = 0
for (var i = 0; i < arr.length; i++) {
if (arr[i] === true) {
count += 1
}
}
console.log(count)
return count
}
Answer (19)
function countTrue(arr) {
let counter = 0
for (let i = 0; i < arr.length; i++) {
if (arr[i]) {
counter++
}
}
return counter
}
Answer (20)
function countTrue(arr) {
var count=0;
arr.forEach(value =>{
if(value)
{
count++;
}
})
return count;
}
Answer (21)
function countTrue(arr) {
return arr.map( e=> e ? true : false).reduce( (pre, cur)=> pre+cur, 0)
}
Answer (22)
function countTrue(arr) {
return arr.map( e=> e ? true : false).reduce( (pre, cur)=> pre+cur, 0)
}
Answer (23)
function countTrue(arr) {
function isTrue(e){
return e===true}
return arr.filter(isTrue).length
}
Answer (24)
function countTrue(arr) {
if (arr.length == 0) return 0;
let sum = 0;
for (const i in arr) {
sum += (arr[i] == true) ? 1 : 0;
}
return sum;
}
Answer (25)
function countTrue(arr) {
var count = 0;
for(var i = 0; i < arr.length; ++i){
if(arr[i] == true)
count++;
}
return count;
}