Home > other >  Infinite-loop problems
Infinite-loop problems

Time:08-07

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:

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)

CodePudding user response:

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

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

  if (i % param1 == 0 && i != param4) {
      console.log(i);
    }
  • Related