Smallest Common Multiple - Big numbers

Smallest Common Multiple - Big numbers
0

#1

Tell us what’s happening:
Hello,
I’m trying to solve Smallest Common Multiple Algorithm, and it works fine with lower numbers
smallestCommons([1, 5]) should return a number.
Passed
smallestCommons([1, 5]) should return 60.
Passed
smallestCommons([5, 1]) should return 60.
Passed
smallestCommons([2, 10]) should return 2520.

But I get undefined when I try to test:
smallestCommons([1, 13])
smallestCommons([23, 18])

Any ideas?

Your code so far


function smallestCommons(arr) {
  let allNums = [];
  let firstInArr = arr[0];
  let lastInArr = arr[arr.length - 1];
  let counter = 0;
  let smallestCommon = 0;
  // Create sorted array with all numbers
  if(firstInArr <= lastInArr) {
    for(let i = firstInArr; i < lastInArr + 1; i++) {
      allNums.push(i);
    }
  }
  else if(firstInArr >= lastInArr) {
    for(let y = lastInArr; y < firstInArr + 1; y++) {
      allNums.push(y);
    }
  }
  // Find smallest common multiple evently divisible
  for(let x = allNums[allNums.length - 1]; x < 10000000; x++) {
    counter = 0;
    allNums.forEach(function(element) {
      if(x % element === 0) {
        smallestCommon = x;
        counter++;
      }
    });
    if(counter === allNums.length) {
      return smallestCommon;
    }
  }
}
console.log(smallestCommons([1, 13]));
smallestCommons([1, 13]);

Your browser information:

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

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


#2

Your solution is inefficient. The FCC test suite “thinks” you have an infinite loop and prematurely stops your code from executing. You need to think about a more efficient algorithm to solve this challenge.


#3

Hi. I tried this first:

function smallestCommons(arr) {
  let arrNum = arr.sort((a,b) => a-b);
  let maxNum = arrNum.pop();
  let minNum = arrNum.shift();
  let notYet = true;
  for(let i = 1; notYet; i++) {
    var multi =  maxNum * i;
    let stillNot = true;
    for (let j = minNum; j<maxNum && stillNot; j++) {
      if (multi % j != 0) {
        stillNot = false;
      }
    }
    if (stillNot) {
      notYet = false;
    }
  }
  return multi;
}

This worked for every test except the last one. I thought it was because the numbers were too big. I looked ahead to the solution, and tried this:

function smallestCommons(arr) {
  arr.sort((a,b) => b-a);
  let newArr = [];
  for (let i = arr[0]; i > arr[1]; i--) {
    newArr.push(i);
  }
  let multi = 1;
  let returnVal = 0;
  let n = 0;

  do {
    returnVal = newArr[0] * multi * newArr[1];
    for (n=2; n<newArr.length; n++) {
      if (returnVal % newArr[n] !== 0) {
        multi++;
        break;
      }
    }
  } while (n !== newArr.length);
  return returnVal;
}

This, as far as I can tell, is functionally exactly the same as the provided solution. Yet, once again, it does not work when the numbers get too big!

I understand the concept of the machine cutting off the execution because it thinks I have an infinite loop. But when I paste the provided solution in there it works fine, and my code does almost exactly the same thing, in almost exactly the same order. Why does it read my code as an infinite loop?

Thanks.