You are viewing a single comment's thread. Return to all comments →
It isn't that much worse in JavaScript, except that JS Arrays don't have sum naturally.
Array.prototype.sum = function(f) { return this.reduce((s, v) => s + f(v), 0); } console.log( apple.sum(d => s - a <= d && d <= t - a)); console.log(orange.sum(d => s - b <= d && d <= t - b));
Or, just with reduce:
console.log( apple.reduce((sum, d) => sum + (s - a <= d && d <= t - a), 0)); console.log(orange.reduce((sum, d) => sum + (s - b <= d && d <= t - b), 0));
Or, by filtering instead:
console.log( apple.filter(d => s - a <= d && d <= t - a).length); console.log(orange.filter(d => s - b <= d && d <= t - b).length);
Apple and Orange
You are viewing a single comment's thread. Return to all comments →
It isn't that much worse in JavaScript, except that JS Arrays don't have sum naturally.
Or, just with reduce:
Or, by filtering instead: