The analyzer has detected a for loop whose counter is not updated in the loop update expression. Instead, a different variable is updated. This looks like a typo that may result in an infinite loop or incorrect termination.
The example:
function findFirst(arr, len, predicate) {
for (let i = 0; i < Math.min(len, arr.length); ++len) {
if (predicate(arr[i]))
return i;
}
return -1;
}
The function should find the first element index in an array of the len length that satisfies the predicate. However, in the loop update expression, the len variable is incremented instead of i. If predicate(arr[0]) is false, the loop will repeatedly check the first element.
The fixed code:
function findFirst(arr, len, predicate) {
for (let i = 0; i < Math.min(len, arr.length); ++i) {
if (predicate(arr[i]))
return i;
}
return -1;
}
This diagnostic rule is classified as:
Was this page helpful?
Your message has been sent. We will email you at
If you do not see the email in your inbox, please check if it is filtered to one of the following folders: