Why does this cycle end in part?

I am trying to write a program to find the smallest common multiple of the parameters provided, which can be evenly divided by both, as well as all sequential numbers in the range between those parameters.

The range will be an array of two numbers, not necessarily in numerical order.

For example, for 1 and 3 - find the smallest common multiple of both 1 and 3 that is evenly divisible by all numbers between 1 and 3.

Why does the loop stop at i = 510,000 (or something close to it) instead of 7,000,000 as I set it?

I also have a screenshot with the output:

function smallestCommons(arr) {
  
  var start;
  var finish;
  var something;
  
  if(arr[0] < arr[1]){start = arr[0]; finish = arr[1];}else{
      start = arr[1]; finish = arr[0];
    }
  
  for(var i = finish;i <= 7000000;i++){
    var boolea = true;
    for(var j = start;j <= finish;j++){
      if(i % j !== 0){boolea = false;break;} // 2 % 1
    }
    
    if(boolea)return i;
    
    something = i;
  }
  
  console.log("final i = " + i);
  
  return 0;
}
      

Run codeHide result


+3


source to share


1 answer


Try adding this to the beginning of the loop

// noprotect

      



it must be that jsbin is forcing your code to exit the loop. See source

+2


source







All Articles