Intermediate Algorithm Scripting: Smallest Common Multiple - can't pass it

Hi all,

I have one problem with Smallest Common Multiple in Intermediate Algorithm Scripting. I developed code, which I tried to run on my local machine and logged results to chrome browser’s console and results are correct - my code works. But still I can’t pass these two challenges:
smallestCommons([1, 13]) should return 360360.
smallestCommons([23, 18]) should return 6056820.

As I said, on my local it returns correct values but still I have cross for these two. So I am thinking it is maybe bug or something?

Here is my code:

function smallestCommons(arr) {
  let max = Math.max(...arr);
  let min = Math.min(...arr);
  var number = 1;

  while(true){

    for(var i = min; i <= max; i++){
      if(number % i != 0){
        break;
      }
      if(i === max){
        return number;
      }
    }

    number++;
  }
}

smallestCommons([1,13]);

Thanks for any feedback and wish you good day!

I’ve seen this type of question a lot recently, so I will just post the link that explains this clearly.
https://forum.freecodecamp.org/t/smallest-common-multiple-help-im-stuck/174012/8

2 Likes

Hi,

You are incrementing your candidate for possible smallest common multiple ( number) by one so most of the numbers you are testing are not even realistic possibilities. You need to eliminate the impossibilities so you are not spending time checking numbers that could never be scm of that range.

I’ll give a small hint. You’re looking for multiples of these numbers. In the case of [1,13] - could 12 , 14 or 15 ever be a candidate?

This is the challenge that seems to break everyone’s cadence. I burned through more than a few sheets of paper working through how to best eliminate impossible candidates. There are formulas available for this kind of thing but what is learned from pinching someone else’s algorithm?

Good luck

1 Like