Smallest Common Multiple, While true loop

Tell us what’s happening:

Well my question is about infinite loops, I want to have an infinite loop that will eventually stop with break. But for some reason, the while is going to certain limits. I don’t know why. For example,

let i = 2;
while(true) {

most IDEs today or call it source editor would have an infinite loop protection, that prevent an unexpected infinite loop to run on the code. an uncontrolled infinite loop can bog down the cpu… which then make the computer unresponsive.
this is probably the case you are having.

Yes, I thought that was the case too. But I am pretty sure that my loop will eventually stop because i have used a break using some condition. The problem is it’s not iterating to the point I want, it prematurely stops.

There doesn’t seem to be any break condition or any logic to stop the loop in the code pasted on the question

Ok here is my actual code:

function smallestCommons(arr) {
  let min = undefined;
  let max = undefined;

  if (arr[0] < arr[1]) {
    min = arr[0];
    max = arr[1];
  } else {
    min = arr[1];
    max = arr[0];

  let i = 2;
  while(true) {
    let multiple = max * i;
    console.log('Multiple', multiple);

    let foundCommonMultiple = true;
    for (let j = min; j <= max; j++) {
      if (multiple % j !== 0) {
        console.log('Not Found', j);
        foundCommonMultiple = false;
    if (foundCommonMultiple) {
      console.log('Returning multiple', multiple);
      return multiple;


I can see that that is a massive operation, logically, that code is ok, I am sure the excution stopped because you are timing out ( codes takes too long ). I would try removing the console log first… it might helps speed up. console.log/IO operation can be expensive.

personally, I wouldn’t use a while loop in there… in some point in time… say beyond the int capacity of multiple = max * i the code will break… knowing that, I usually create a for loop with the range of 2 until maximum number of possible multiple that can be handled by JavaScript. although this is might not required by the challenge, but for future reference only.

1 Like

Just FYI, manually assigning undefined to variables is considered bad practice (as far as I know). If you want to assign an “empty” value null is better. That way you can tell the difference between normal unassigned variables and your own “empty” ones.