Skip to main content

Ivan Teoh

Something personal yet public

171-exercise61a.js

(Source)

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

function reduce(combine, base, array) {
  forEach(array, function (element) {
    base = combine(base, element);
  });
  return base;
}

function zeroes(a, b) {
    if (b) {
        return a;
    }
    return a + 1;
}

function countZeroes(numbers) {
    return reduce(zeroes, 0, numbers);
}

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