Smallest Common Multiple bug

Tell us what’s happening:

When I run the below code on the javascript console in chrome, it works. If however, I run it in FCC I get a cryptic Node error message that reads :

4/commons-b6031e643bcd8c390b24.js:28 TypeError: unknown: Cannot read property ‘0’ of undefined
at Function.e.get (/commons-b6031e643bcd8c390b24.js:32)
at e.h [as unshiftContainer] (/commons-b6031e643bcd8c390b24.js:49)
at t. (/commons-b6031e643bcd8c390b24.js:21)
at r (/commons-b6031e643bcd8c390b24.js:49)
at e.o [as _call] (/commons-b6031e643bcd8c390b24.js:48)
at e.i [as call] (/commons-b6031e643bcd8c390b24.js:48)
at e.a [as visit] (/commons-b6031e643bcd8c390b24.js:48)
at e.visitQueue (/commons-b6031e643bcd8c390b24.js:47)
at e.visitMultiple (/commons-b6031e643bcd8c390b24.js:47)
at e.visit (/commons-b6031e643bcd8c390b24.js:47)
(anonymous) @ /commons-b6031e643bcd8c390b24.js:28

Your code so far


  function isPrime(n)
  {
    let arr = [];
    for (let i = 2; i < n; i++)
      if (n % i === 0) return false;
    return true;
  }

  function getPrimes(n)
  {
    let arr = [];
    for (let i = 2; i <=n; i++)
      if (isPrime(i)) arr.push(i)
    return arr;
  }

  function primeFactorize(n)
  {
    let factors = []
    let k = n;

    if (isPrime(n)) return [{base: n, pow:1}];
    for (let i = 0; i < PRIMES.length; i++)
      if( n % PRIMES[i] === 0 ) {
        console.log("new Prime found " + PRIMES[i]);
        factors.push({base:PRIMES[i], pow:1})}
    for (let i = factors.length; i--; i>-1)
    {
      let count  = 0;
      let j = factors[i].base;

      while (k % j === 0)
      {
        count++;
        k /= j;
      }
      k *= j;
      factors[i].pow = count;
    }

    return factors;
  }

  function smallestCommons(arr) {
    const {min, max} = Math;
    const j = min(...arr) === 1 ? 2 : min(...arr);
    const k = max(...arr);
    let range = [];
    let allFactors = [];
    let bases=[], maxes=[];
    for (let i = j; i<=k; i++)
    {
      allFactors.push(...primeFactorize(i));
    //  console.log(primeFactorize(i));
      range.push(i);
    }
  //  console.log(allFactors);
 allFactors = allFactors.sort(((a,b) =>
    {
      return a.base - b.base
    }));
    //console.log(allFactors);
    //console.log(allFactors.splice(allFactors.findIndex(o => {return o.base === 3})-1))
    let maxIndex = 0;
//console.log(allFactors);

  allFactors.forEach((f) => {if (bases.indexOf(f.base)===-1) bases.push(f.base)});

    bases.forEach(base =>
    {
      //console.log(allFactors);
      let max = {};
      let nextBase = bases[bases.indexOf(base)+1];
      maxIndex = allFactors.findIndex((o) => {return o.base === nextBase});
      if(nextBase) {
        max = allFactors.slice(0, maxIndex).sort((a, b) => {
          return b.pow - a.pow
        });
        //console.log("max :" + JSON.stringify(max), "maxIndex:" + JSON.stringify(maxIndex))
        allFactors = allFactors.splice(maxIndex);
      }
      else max = allFactors;

      let most = 0;
      while (max.length > 0) most = max.pop();
      maxes.push(most);}
  );
    //console.log(maxes);
    let product = 1;
    maxes.forEach(obj => {product *= Math.pow(obj.base, obj.pow)})

    return (product);


  }

  const PRIMES = getPrimes(23);

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Mobile Safari/537.36.

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