Smallest Common Multiple - Big numbers

Smallest Common Multiple - Big numbers
0

#1

Tell us what’s happening:
Hello,
I’m trying to solve Smallest Common Multiple Algorithm, and it works fine with lower numbers
smallestCommons([1, 5]) should return a number.
Passed
smallestCommons([1, 5]) should return 60.
Passed
smallestCommons([5, 1]) should return 60.
Passed
smallestCommons([2, 10]) should return 2520.

But I get undefined when I try to test:
smallestCommons([1, 13])
smallestCommons([23, 18])

Any ideas?

Your code so far


function smallestCommons(arr) {
  let allNums = [];
  let firstInArr = arr[0];
  let lastInArr = arr[arr.length - 1];
  let counter = 0;
  let smallestCommon = 0;
  // Create sorted array with all numbers
  if(firstInArr <= lastInArr) {
    for(let i = firstInArr; i < lastInArr + 1; i++) {
      allNums.push(i);
    }
  }
  else if(firstInArr >= lastInArr) {
    for(let y = lastInArr; y < firstInArr + 1; y++) {
      allNums.push(y);
    }
  }
  // Find smallest common multiple evently divisible
  for(let x = allNums[allNums.length - 1]; x < 10000000; x++) {
    counter = 0;
    allNums.forEach(function(element) {
      if(x % element === 0) {
        smallestCommon = x;
        counter++;
      }
    });
    if(counter === allNums.length) {
      return smallestCommon;
    }
  }
}
console.log(smallestCommons([1, 13]));
smallestCommons([1, 13]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple/


#2

Your solution is inefficient. The FCC test suite “thinks” you have an infinite loop and prematurely stops your code from executing. You need to think about a more efficient algorithm to solve this challenge.