Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold LeetCode solution in JavaScript
1var numOfSubarrays = function (arr, k, threshold) {2let total = 0;3let left = 0;4let right = k;5// get initial sum of numbers in first sub array range, by summing left -> right6let sum = arr.slice(left, right).reduce((a, b) => a + b, 0);7while (right <= arr.length) {8// move through the array as a sliding window9if (left > 0) {10// on each iteration of the loop, subtract the val that has fallen out of the window,11// and add the new value that has entered the window12sum -= arr[left - 1];13sum += arr[right - 1];14}15if (sum / k >= threshold) {16total++;17}18left++;19right++;20}21return total;22};