So I created these functions to solve this algorithm…It really took me a while to figure it out but I tested it on my own custom HTML/JS and i got all the answers, yet when i test it on FCC’s it says I have an infinite loop…Can some one help me please! This one has really kicked my butt!
function lc(num1,num2){
var boo = false;
var i = 1;
while(!boo){
if(i % num1 === 0){
if(i % num2 === 0){
return i;
}
}
i++;
}
}
function sortArr(arr) {
arr = arr.sort(function(a,b){return a-b;});
var j = 0;
for(var i=0; i < arr[arr.length-1]; i++ ){
if(i > arr[j]){
arr.push(i);
arr = arr.sort(function(a,b){return a-b;});
j++;
}
}
return arr;
}
function smallestCommons(arr){
arr = sortArr(arr);
var result = lc(arr[arr.length-2], arr[arr.length-1]);
for(var i=arr.length-3; i >= 0; i--){
if(arr[i] === undefined){
return result;
}
result = lc(result, arr[i]);
}
return result;
}
// lc(18,2018940)
smallestCommons([23, 18]);