* fix: restructure certifications guide articles * fix: added 3 dashes line before prob expl * fix: added 3 dashes line before hints * fix: added 3 dashes line before solutions
994 B
994 B
title
title |
---|
Prevent Infinite Loops with a Valid Terminal Condition |
Prevent Infinite Loops with a Valid Terminal Condition
Problem Explanation
- To prevent an infinite loop, the
while-condition
must reach a terminal condition to exit out of the loop. - So the error in this challenge occurs due to the condition -
i != 4
- in the for loop. - If you take a closer look at the code:
function myFunc() {
for (let i = 1; i != 4; i += 2) {
console.log("Still going!");
}
}
- You will see that
i
is first initialised as 1 and after every iteration of the loop,i
is incremented by 2. - Using this logic, after the first iteration -
i = 3
and the second iterationi = 5
, the conditioni != 4
will never be met and an infinite loop will occur.
Solutions
Solution 1 (Click to Show/Hide)
function myFunc() {
for (let i = 1; i <= 4; i += 2) {
console.log("Still going!");
}
}