Smallest Common Multiple0

Tell us what’s happening:
Help me with explanation or help me find where am i wrong. The below 3 cases test fails.

smallestCommons([2, 10]) should return 2520.

smallestCommons([1, 13]) should return 360360.

smallestCommons([23, 18]) should return 6056820.

THANKS in advance

Your code so far


function smallestCommons(arr) {
  var r = arr.sort();
  var arr1 = [];
  for(var i = arr[0];i<= arr[arr.length-1];i++) {
    arr1.push(i);
  }
  var x = true;
  var lcm = 0;
  while(x) {
     lcm++;
     for(var j = arr1[0];j <= arr1[arr1.length-1];j++) {
        if(lcm % j !== 0) {
           break;
        }
        else if(j === arr1[arr1.length-1]) {
          x = false;
        }
      }
  };
  //console.log(lcm);
  return lcm;
}


smallestCommons([2,10]);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0.

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

your issue is actually here, arr1 is an empty array, so lcm start getting higher and higher and higher and triggering the infinite loop protection, and return the wrong number

comment out the while loop, and try making sure that the part before that works.

be sure that you know how the sort method works


the other part of the issue, once the for loop start working, is that you are checking the numbers one by one and that is too resource intensive when you need to arrive in the millions range and is also stopped by the infinite loop protection