The problem I'm trying to solve is to calculate least common multiple of a given range of numbers. The numbers supplied in the array can be in any order, but the attay will only have two numbers.
My code works for pairs [1,5] , [1,10],[1,12], but once I try [1,13] i'm getting infinite loop error and answer is becoming undefined.
For below code , expected answer is 360360
function smallestCommons(arr) {
var a = arr[0];
var b = arr[1];
var start = 0;
var end = 0;
if (a<b) {
start=a;
end=b;
}
else {
start=b;
end=a;
}
//console.log("Start:"+start);
//console.log("End:"+end);
var numArray = [];
for(let i=start; i<end+1; i++) {
numArray.push(i);
}
console.log(numArray);
var oddArray = [];
var product = numArray.reduce((a,b)=>a=a*b);
console.log("Product:"+product);
console.log(numArray[0]);
var result = 0;
console.log(result);
//let j=numArray[0];
for(let j=numArray[0];j<=product;j++) {
var count = 0;
for(let k=0;k<numArray.length; k++) {
if(j%numArray[k]==0) {
count++;
}
}
if(count==numArray.length) {
return j;
}
}
}
console.log(smallestCommons([1,13]));