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

Infinite-loop problems

I was doing some challenges from Coding.Dojo algorithm challenges and I’m stuck at "The Final Countdown" challenge. So this is what I was asked to do:

Give 4 parameters (param1, param2, param3, param4),print the multiples
of param1, starting at param2 and extending to param3.If a multiple is
equal to param4 then skip-don’t print that one. Do this using a while.
Given (3,5,17,9) print 6,12,5 (which are the multiples of 3 between 5
and 17, except of the value 9).

The problem is that when I run my code it goes to an infinite loop. Can anyone tell me what did I do wrong. Here’s my code:

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

function finalCount(param1, param2, param3, param4) {
  var i = param2;
  while (i <= param3) {
    if (i == param4) {
      continue;
    } else if (i % param1 == 0) {
      console.log(i);
    }
    i++;
  }
}
finalCount(3, 5, 17, 9)

>Solution :

Calling continue will put you into an infinite loop because i does not get incremented by one.

You can move invert the check i == param4 and put it into the second condition like this:

  if (i % param1 == 0 && i != param4) {
      console.log(i);
    }
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