Smallest Common Multiple maybe a bug?

Hi,

I think I solved this exercise but the last condition is still unchecked.

I used the console to check what it was being returned, and it returns the same as the requirement. So I am confused whether it’s a bug or am I missing something?

These are the screenshots:


This is my code:

function smallestCommons(arr) {
  if (arr[0] < arr[1]){
    var a = arr[0];
    var b = arr[1];
  } else{
    var a = arr[1];
    var b = arr[0];
  }
  var commonMultiple = 0;
  for (var i = 1; commonMultiple === 0; i++){
    if( i % a === 0 && i % b === 0){
      commonMultiple = i;
      for (var j = a + 1; j < b; j++){
        if(commonMultiple % j != 0){
          commonMultiple = 0;
          j = b;
        }
      }
    }
  }
  return commonMultiple;
}


smallestCommons([1,5]);

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

That usually means that the exercise has timed out, that your code isn’t efficient enough.

Yes, the test fail message should be better.

1 Like

Then I have to find another way to make it “shorter” and “faster” to pass it?

Faster, yes, more efficient.

This is a common difficulty that people have on this challenge. Yes, there are brute force ways to solve it, but there are also more efficient ways.

1 Like