Steps to get the solution:
1. Create two variables 'currentCount' and 'maxCount' and initialise them with 0
2. Loop over array items
3. if array item value is 1 then increment currentCount
4. Set maxCount as maximum of currentCount and maxCount
5. if array item value is not 1, then make currentCount as 0
6. return maxCount after the end of loop
Code :
function maxCount(arr) {
let maxCount = 0;
let currentCount=0;
for(let i=0;i<arr.length;i++) {
if(arr[i]==1) {
currentCount += 1;
maxCount = Math.max(currentCount, maxCount);
}
else {
currentCount = 0;
}
}
return maxCount;
}
let arr = [1,0,0,1,1,1,0,0,1,0,1,1,0,0,1];
let result = maxCount(arr);
console.log('result', result); //output: 3