function solution(M, arr) {
let i = 0,
sum = 0,
q = 0;
answer = 0;
for (i; i < arr.length; i++) {
sum += arr[i];
if (sum < M) {
answer++;
} else {
if (sum === M) answer++;
q++;
i = q - 1;
sum = 0;
}
}
return answer;
}
let M = 5;
let arr = [1, 3, 1, 2, 3];
console.log(solution(M, arr));