Smallest Common Multiple issues

Smallest Common Multiple issues
0

#1

Can’t seem to figure out what is going wrong. I pass all the tests except for the last one [23,18].

function smallestCommons(arr) {
  var range = [], i, checker;
  //sort by smallest to largest number;
 i = arr.sort(function (a, b) {
    return a - b;
});
  var j = 1;
  for(var k = i[0]; k <= i[1]; k++){
    if((i[1] * j) % k === 0){
      checker = (i[1]) * j;
    } else{
      k = i[0];
      j++;
    }
  }
  
  return checker;
}

The number it gives me is divisible by all of the numbers in the range EXCEPT 18 - but I don’t understand why as 18 should be the first number it performs the check on…


#2

You need to throw so console.log statements in to check your assumptions about the the values are at various points in your code. Then you should be able to see the flaw with your logic.


#3

Finally solved this in case anyone else had the same issue - It looks like my original code had been checking the multiples in ascending order - check the range. Changed the range to a descending order and all was well. I think that checking the multiples in ascending order simply took too long or took too much memory, and the browser simply couldn’t cut it.