const calcLength = (nums, k) => {
let left = 0;
let right = 0;
let result = 0;
let zeroCount = 0;
 
while (right < nums.length) {
if(nums[right] === 0) {
zeroCount++
}
if(zeroCount > k) {
if(nums[left] === 0) {
zeroCount--
}
left++;
continue
}
if (right - left + 1 > result) {
result = right - left + 1;
}
right++
}
 
return result;
};
 
console.log(calcLength([1,1,1,0,0,0,1,1,1,1,0], 2))
 
Click Run to execute JavaScript.
Code runs in an isolated environment.