Smallest Common Multiple - possible bug

Smallest Common Multiple - possible bug
0

#1

Hi everyone, this code is failing on test cases [1, 13] and [23, 18]. I’ve tested it on two other Node editors and it works. Would appreciate some help.

function smallestCommons(arr) {
  arr = arr.sort((a, b) => a - b);
  const range = [];

  for(let i = arr[0]; i <= arr[1]; i++) {
    range.push(i);
  }
  const product = range.reduce((acc, curr) => acc * curr);
  for(let i = range[range.length - 1] + 1; i <= product; i++) {
    if(range.every(num => i % num == 0)) {
        return i;
    }
  }
}

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


#2

helps to add a link to the challenge…


#3

I wonder if the reason it fails is because the challenge expects the code to be more efficient…
I had heard that there were such requirements but I’m not sure what they are. Did you try searching the forum to see if this is mentioned?


#4

Tried the suggestion here but it still doesn’t work.


#5

maybe you can try to rewrite to make it more efficient? (for eg. using the greatest common divisor method?)