public
Created

Functional Programming in JavaScript - 6

  • Download Gist
gistfile1.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
var forEach = function (list, action) {
for (var i = 0; i < list.length; i++) {
action(list[i]);
}
};
 
var reduce = function (combine, base, list) {
forEach(list, function (item) {
base = combine(base, item);
});
return base;
};
 
var countNegativeNumbers = function (negativeNumbersTillNow, currentNumber) {
if (typeof currentNumber === "number" && currentNumber < 0) {
negativeNumbersTillNow += 1;
}
return negativeNumbersTillNow;
};
 
var initialCount = 0;
 
console.log(reduce(countNegativeNumbers, initialCount, [1, -1, 0, 45, "-42", -42]));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.