I need to calculate the sum of all k-sized sub-arrays in an array using sliding window algorithm. Is that a valid sliding window algorithm? If not, why?
var sumOfSubArrays = function(arr, k) {
let currentSubArray = 0;
for(let i=0; i<k; i++) {
currentSubArray += arr[i];
}
let sum = currentSubArray;
for(let i=0; i<arr.length-k; i++) {
current = currentSubArray - arr[i] + arr[i+k];
sum += current;
currentSubArray = current;
}
return sum;
};
let arr = [1,2,3,4,5]
let k = 3;
console.log(sumOfSubArrays(arr, k));
Additionally, what could I improve in this code?