Smallest Common Multiple Algorithmic Paradigm

Hello,

For the Smallest Common Multiple assignment I came up with the following code that is currently working for all testing scenarios. However, I wonder whether this code, that I assume is using Brute-force search algorithmic paradigm by checking every possible option of common multiple for all array entries until it finds one, would be considered as “bad” coding practice? What other algorithms could be used to make the code more efficient?

My code:


function smallestCommons(arr) {
  let nums = [];
  if (arr[1] >= arr[0]) {
    for (let i=arr[0]; i <= arr[1]; i++) {
      nums.push(i);
    }
  } else {
    for (let i=arr[1]; i <= arr[0]; i++) {
      nums.push(i);
    }
  }
  let smallestCommon = 0;
  let j=1;
  while (smallestCommon == 0) {
    let commonMultipleCount = 0;
    for (let k=nums[0]; k <= nums[nums.length-1]; k++) {
      if (j % k == 0) {
        commonMultipleCount++;
      }
      if (commonMultipleCount == nums.length) {
        smallestCommon = j;
        break;
      }
    }
    j++;
  }
  return smallestCommon;
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.

Challenge: Smallest Common Multiple

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple

If you click on the Get a Hint button on the challenge, you will find that the 2nd solution is quite efficient.

I’m a bit confused about this second solution. As I understand the gcd function is using recursion to find greatest common divisor, but I’m not quite sure what this part of code is doing:

var GCD = gcd(lcm, range[i]); lcm = (lcm * range[i]) / GCD;

Especially the lcm part.