I have a binary array and would like to count the longest number of 1s in a row with this function. In particular, when I use this value for nums like [1,1,0,1]
it counts correctly.
let nums = [1, 1, 0, 1, 1, 1];
var findMaxConsecutiveOnes = function(nums) {
let val = 0;
if (nums.length >= 1) {
console.log(nums.sort().reverse());
for (i = 0; i <= nums.length - 1; i++) {
if (nums[i] === nums[i + 1]) {
val++;
} else {
console.log(val);
return val;
}
}
console.log(val);
return 0;
}
};
findMaxConsecutiveOnes(nums)