ds-algo-study
Version:
Just experimenting with publishing a package
41 lines (33 loc) • 1.11 kB
JavaScript
/*******************************************************************************
Write a function `count` that accepts an array and a callback as arguments. The
function should return the number of elements of the array that return true when
passed to the callback.
Examples:
let result1 = count([18, 5, 32, 7, 100], function (n) {
return n % 2 === 0;
});
console.log(result1); // 3
let result2 = count([17, 5, 31, 7, 100], function (n) {
return n % 2 === 0;
});
console.log(result2); // 1
let result3 = count(['follow', 'the', 'yellow', 'brick', 'road'], function (str) {
return str.includes('o');
});
console.log(result3); // 3
let result4 = count(['follow', 'the', 'yellow', 'brick', 'road'], function (str) {
return str.includes('x');
});
console.log(result4); // 0
*******************************************************************************/
function count(array, cb) {
let numTrue = 0;
array.forEach(function(el) {
if (cb(el)) {
numTrue++;
}
});
return numTrue;
}
//******************---------------------******************\\*/
module.exports = count;