3

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

The range will be an array of two numbers that will not necessarily be 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 that) 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;
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Narcis Neacsu
  • 1,013
  • 2
  • 12
  • 19

1 Answers1

2

Try to add this at the beginning of your loop

// noprotect

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

Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82