Smallest Common Multiple - test bug?

Tell us what’s happening:

“Smallest Common Multiple” challenge:

The following code will not pass test on the last test ([23, 18]), but node.js will! Please help!

Your code so far


function smallestCommons(arr) {
  arr1 = arr.sort();
  var nums = [];
  for (var i = arr1[0]; i <= arr1[1]; i++) {
    nums.push(i);
  }

  var nextNum = 1;

  function lcmChecker(val) {
    return nextNum % val == 0;
  }

  while (true) {
    if (nums.every(lcmChecker)) {
      return nextNum;
    }
    nextNum++;
  }

}
smallestCommons([1,5]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/smallest-common-multiple

Thanks so much for your help!! The bug didn’t happen in safari FYI.

Hi,
It’s worth noting that the default sort order for sort() is according to string Unicode code points, so you can easily get incorrect results with numbers. E.g.

[1500, 2, 4].sort() //[ 1500, 2, 4 ]

Passing a comparison function to sort() can solve this. See MDN for more.

I wonder if your algorithm would be better to not depend on an array, as this means holding a potentially large array of numbers in memory.