Smallest Common Multiple can't pass on 2 tests

Smallest Common Multiple can't pass on 2 tests
0
#1

Tell us what’s happening:
Can’t pass on 2 test. Aparently when the SCM is too big, the system stops processing the code before it reach the SCM. Probably an infinite loop protection.

Your code so far


function smallestCommons(arr) {

  let found = false;
  let mmc = 0;
  arr = [].concat(arr).sort((a,b) => a - b)

  do{

    mmc++;

    for(let i = arr[0];i <= arr[1]; i++){
      if(mmc % i !== 0) break;    
      if(i == arr[1]) found = true
    }

  }while (!found)
  
  return mmc;
}


console.log(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/74.0.3729.157 Safari/537.36.

#2

Looks like you’ll need a different approach to the problem. A for loop incrementing by 1 will be wildly inefficient for this problem. I struggled on this one too.

This is a math-heavy problem and you’ll need to do some research on things like greatest common denominator and prime factorization.

1 Like