Smallest Common Multiple - please help

Tell us what’s happening:
Hi All,
I can get this to pass all tests except for the last one, can someone please help!

Your code so far


function smallestCommons(arr) {
  arr.sort((a,b) => a-b);
  var indicator = false;
  var commonMulti = arr[1];

  do {
    indicator = true;
    for (var i = arr[0]; i <=arr[1]; i++){
    if(commonMulti % i != 0){
        commonMulti++
        indicator = false
      } 
    }
  } while (!indicator)
  
  return commonMulti

}


console.log(smallestCommons([23,18]));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) 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

Hey @Hobbo ,
Your code needs to be efficient to pass the challenge.

Your code is activating the FCC’s infinite loop protection.

So, try to make your code a little efficient.

All the best.

2 Likes

Let

SCM(a, b) = Smallest Common Multiple
GCD(a, b) = Greatest Common Divisor

We know that

a * b = SCM(a, b) * GCD(a, b) =>
SCM(a, b) = a * b / GCD(a, b)

GCD can be computed in a very efficient way using Euclid’s algorithm: https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations

 
 
 

Mike
Coding and System Design interview practice