Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Functional Programming in JavaScript - 6

View gist:1519449
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]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.