31 lines
1.2 KiB
Plaintext

== Why is this an issue?
In a `for` loop, the update clause is responsible for modifying the loop counter variable in the appropriate direction to control the loop's iteration. It determines how the loop counter variable changes with each iteration of the loop. The loop counter should move in the right direction to prevent infinite loops or unexpected behavior.
This rule raises an issue when the loop counter is updated in the wrong direction with respect to the loop termination condition.
[source,javascript,diff-id=1,diff-type=noncompliant]
----
for (let i = 0; i < strings.length; i--) { // Noncompliant: The counter 'i' is decremented, making the loop infinite
//...
}
----
To ensure the `for` loop behaves as expected, you should specify the correct update clause that moves the loop counter in the right direction based on the loop's logic and desired outcome.
[source,javascript,diff-id=1,diff-type=compliant]
----
for (let i = 0; i < strings.length; i++) {
//...
}
----
== Resources
=== Documentation
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for[``++for++``]
* Wikipedia - https://en.wikipedia.org/wiki/Infinite_loop[Infinite loop]
include::../rspecator.adoc[]