UNPKG

findstreak

Version:

In an array, find the N consecutive items whose combined value is highest. Or lowest.

21 lines (20 loc) 503 B
module.exports = function(a, n, cf, lo) { if (a.length < n) { return a; } var currentArray = a.slice(0, n); var currentValue = currentArray.reduce(cf, 0); for (let i=0; i<=a.length-n; i++) { let tmp = a.slice(i, i+n); let value = tmp.reduce(cf, 0); if (lo && value < currentValue) { currentArray = tmp; currentValue = value; } else if (!lo && value > currentValue) { currentArray = tmp; currentValue = value; } } return currentArray; };