Smallest Common Multiple: Console.log breaks loop or something!

Tell us what’s happening:

Your code so far

function smallestCommons(arr) {
  arr.sort(function(a, b) {
    return b - a;

  var newArr=[];
  for(var i=arr[0];i>=arr[1];i--) {
  // Variables needed declared outside the loops.
  var quot = 0;
  var loop = 1;
  var n;

  // Run code while n is not the same as the array length.
  do {
    quot = newArr[0] * loop * newArr[1];
    for (n = 2; n < newArr.length; n++) {
      if (quot % newArr[n] !== 0) {
  } while (n !== newArr.length);
  return quot;

smallestCommons([1, 13])

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36.

Link to the challenge:

This code returns error “smallestCommons([23, 18]) should return 6056820”
If i remove the line “console.log(quot);” however, it works. Please help me understand this behaviour? Thank you.

FCC uses a timeout as an infinite loop/recursion protection (so campers don’t crash their browsers). It sounds like you solution is right on the edge of being efficient enough to finish within the timeout, so adding the extra command of a console.log prevents it from finishing in enough time.

1 Like

Thanks so much. I googled the hell out of this and was none the wiser :grin: Crystal clear now. :slight_smile:

Glad I could help. Happy coding!

1 Like

Thank you. While I have your attention , can i trouble u with a math question? can you guide me to some resource on a section of the logic used above? Specifically, why is the LCM always a multiple of the product of the highest two numbers in the array. Is this being assumed as most probable, or is this always true for two consecutive numbers? :thinking:

The LCM will always be a multiple of the two highest numbers because the LCM must be a divisible by all numbers in the group, including the last two.

For example: 3, 4, 5 must be divisible by 4 and 5. The smallest number that is is divisible by 4 and 5 is 20. That isn’t divisible by 3, so we have to check a larger number. Since it still must be divisible by 4 and 5, the next smallest number we could check is 2*4*5 which is the same as 2*20. And so on.

Maybe expirimenting with something like an LCM calculator might help? I think I’ve seen some that even step through the logic for more than 2 numbers.

1 Like

Thanks for the reply :smiley: … I understand that this seems to be true for easily countable numbers but I’m looking to find some proof for the underlying logic as to why we are confidently skipping over all the numbers between the multiples. I get that the lcm is the product of prime powers,maybe that has something to do with it. Anyhow, I’m chasing down all the links on the lcm wikipedia page just to be sure I have a genuine question here :laughing: , then I’ll bother all the math forums.Thanks so much for ur reply and input. :smiley:
P.S. I find math fascinating but it aint exactly my forte :laughing:
Thanks for the calculator link too. Will come handy :+1::+1:

I’m not entirely sure what you mean by “we are confidently skipping over all the numbers between the multiples”, but maybe this StackExchange thread is what you’re looking for.

1 Like

i will follow that link , thank you…i mean that if the array is of larger non-prime numbers, is it not possible that the lcm of a given range may not be a multiple of the largest two numbers only? If the numbers share factors? Conversely I find it fascinating that this is so if always true. There is probably a trivial mathematical explanation that i just cant comprehend for now. I shoulda paid more attention in high school :laughing: Thanks again for ur help :smiley: