secret
Created

Eloquent JavaScript

  • Download Gist
6.1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'use strict';
 
var forEach = function (array, action) {
for (var i = 0; i < array.length; i++) {
action(array[i]);
}
};
 
var reduce = function (combine, base, array) {
forEach(array, function (element) {
base = combine(base, element);
});
 
return base;
};
 
var count = function (test, numbers) {
return reduce(function (total, number) {
if (number === test) {
return total + 1;
}
return total;
}, 0, numbers);
};
 
var countZeroes = function (numbers) {
return count(0, numbers);
};
 
// Test!
console.log( countZeroes([5, 6, 0, 3, 5, 7, 0, 0, -0]) );

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.