Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does the following code cause an infinite loop ? And how to fix the problem

let index = 10;
let jump = 2;

for (;;) {
  // Write Your Code Here
     let i = index;
     console.log(i);
     i -= jump;
     if (i === jump) {
         break;
     }
 }

I think that I know the logic of the code, but I don’t understand why it does loop infinitely

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

maybe the following code fixes the problem.

let index = 10;
let jump = 2;
let i = index;

for (;;) {
  // Write Your Code Here
     
     console.log(i);
     i -= jump;
     if (i === jump) {
         break;
         console.log('broken...')
     }
 }

The reason is variable i is being refreshed with static index value 10 every time it loops. Hence it never breaks.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading