JSFiddle - React, Tailwind, and code Playground
by Steven Senkus
JavaScript
function maxSumConsecutiveIntegers(arr, window) {
let maxSum = 0;
for (let i = 0; i <= arr.length - window; i ++) {
/* console.log(arr[i]) */
let windowSum = arr[i];
for (let j = 1; j < window; j++) {
windowSum += arr[i + j];
}
/* console.log('windowSum', windowSum) */
if (windowSum > maxSum) maxSum = windowSum;
}
return maxSum
}
const ints = [100,200,300,400];
console.log(maxSumConsecutiveIntegers(ints, 2));
/* const ints = [5,2,-1,0,3];
console.log(maxSumConsecutiveIntegers(ints, 3)); */
/* const moreInts = [ 1, 4, 2, 10, 2, 3, 1, 0, 20];
console.log(maxSumConsecutiveIntegers(moreInts, 4)); */