Is this a better Smallest Common Multiple solution?

Tell us what’s happening:

There’s actually nothing wrong with my code. One of three things is happening though, because I am getting a different solution for larger argument sets than the Advanced Code Solution (sample input: [50,75]):

  1. My code is flawed for the problem, though I have hand-checked my solution to the input above, and it appears correct. (solution: 4,642,728,255,985,660,000)

  2. The Advanced Code Solution is flawed, because it gives a different and larger answer to the same input, though the input is divisible by all numbers in the input range. (solution: 162,495,488,959,498,100,000)

  3. There is something wrong with the Euclidian algorithm or how it is being applied. My code uses a different method to find the solution, but I can’t imagine that my method would be more efficient than something as old and studied as the Euclidean algorithm.

TL;DR I am just trying to find if there is an error in my method. It passes the test cases, but larger data sets find different solutions than the sample solutions on freeCodeCamp.com

Your code so far


function smallestCommons(arr) {

  //First find the range of integers we are working with from the arguments
  let rangeArray = [];

  for (let i = Math.min(...arr); i <= Math.max(...arr); i++) {
    rangeArray.unshift(i);
  };

  // Next find all the primes up to and including the range max 
  let primeArray = [];

  for (let primeTest = 2; primeTest <= rangeArray[0]; primeTest++) {
    let notPrime = false;
      for (let testInt = 2; testInt <= rangeArray.length; testInt++) {
        if (primeTest % testInt === 0 && primeTest !== testInt) {
          notPrime = true;
        }
      } if (notPrime === false) {
      primeArray.push(primeTest);
    }
  };

  // Now test to see what ALL the prime factors (including powers) are for each integer in the range and push them into their own array
  let arrayOfPrimeFactors = [];
  
  for (let rangeIndex = 0; rangeIndex < rangeArray.length; rangeIndex++) {
    let factoredArray = [];
    let testRangeInt = rangeArray[rangeIndex];
    for (let primeIndex = 0; primeIndex < primeArray.length; primeIndex++) { 
      while (testRangeInt % primeArray[primeIndex] === 0) {
        factoredArray.push(primeArray[primeIndex]);
        testRangeInt = testRangeInt / primeArray[primeIndex];
      } 
    } 
    arrayOfPrimeFactors.push(factoredArray);
  };

  //Create an object to keep count of the largest powers of primes from our arrayofPrimeFactors

  let largestPrimePowers = {};

  //Iterate through the array of arrays of prime factors to find the largest power of each prime factor and log those into the object

  for (let primeFactorsIndex = 0; primeFactorsIndex < arrayOfPrimeFactors.length; primeFactorsIndex++) {
    for (let itemIndex = 0; itemIndex < arrayOfPrimeFactors[primeFactorsIndex].length; itemIndex++) {
      if (!largestPrimePowers.hasOwnProperty(arrayOfPrimeFactors[primeFactorsIndex][itemIndex])) {
        largestPrimePowers[arrayOfPrimeFactors[primeFactorsIndex][itemIndex]] = 1;
      } else if (arrayOfPrimeFactors[primeFactorsIndex].reduce((x, value) => x + (value === arrayOfPrimeFactors[primeFactorsIndex][itemIndex]), 0) > largestPrimePowers[arrayOfPrimeFactors[primeFactorsIndex][itemIndex]]) {
        largestPrimePowers[arrayOfPrimeFactors[primeFactorsIndex][itemIndex]] = arrayOfPrimeFactors[primeFactorsIndex].reduce((x, value) => x + (value === arrayOfPrimeFactors[primeFactorsIndex][itemIndex]), 0)
      }
    }
  };

  console.log(largestPrimePowers);

  //Turn the object into an array of arrays
  let primePowersArray = Object.entries(largestPrimePowers);

  //Time to turn our array into some Math
  let answer = primePowersArray.reduce((acc, value) => acc * Math.pow(Number(value[0]), value[1]), 1);

  //Return the answer FTW
  console.log(answer)
  return answer;

}

smallestCommons([55,70]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36.

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

The easiest and probably most elegant way of solving this problem is noting the identity
a*b = gcd(a,b) * lcm(a,b) => lcm(a,b) = a*b/gcd(a,b) also note that lcm(a,b,c) = lcm(lcm(a,b),c).