Smallest Common Multiple - test bug?

Smallest Common Multiple - test bug?
0

#1

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


#2

You are experiencing the same problem another camper had earlier this morning. See the following thread regarding how the FCC tests think you have an infinite loop and how to deal with it.


Smallest Common Multiple - possible bug
#3

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


#4

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.