Skip to content

@Srirangan /gist:1519449
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Functional Programming in JavaScript - 6
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.