function solution(arr, m) {
let p = 0,
q = 1,
answer = 0;
let sum = arr[0];
while (p < arr.length && q < arr.length) {
sum += arr[q];
if (sum >= m) {
if (sum === M) answer++;
p++;
q = p + 1;
sum = arr[p];
} else {
q++;
}
}
return answer;
}
let arr = [1, 2, 1, 3, 1, 1, 1, 2];
let M = 6;
console.log(solution(arr, 6));