Skip to main content

Ivan Teoh

Something personal yet public

171-exercise61b.js

(Source)

function forEach(array, action) {
    var i;
    for (i = 0; i < array.length; i++) {
        action(array[i]);
    }
}

function count(array, test) {
    var total = 0;
    forEach(array, function (element) {
        if (test(element)) {
            total += 1;
        }
    });
    return total;
}

function zeroes(a) {
    return !a;
}

function countZeroes(numbers) {
    return count(numbers, zeroes);
}

//document.write(countZeroes([1, 3, 0, 6, 0, 0, 8, 2])); // 3