Hi it's my first question here, Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.
The range will be an array of two numbers that will not necessarily be in numerical order.
For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers between 1 and 3. The answer here would be 6.
Ive tried the following code but I dont know why its not working...
function smallestCommons(arr) {
arr.sort(function(a,b){return a-b});//sorts the array
/* Prints a sorted Array*/
let sortArr=[];
for(let i=arr[0];i<=arr[1];i++){
sortArr.push(i);
}
/*Find Largest Possible LCM*/
let largeCM=1;
for(let i=0;i<sortArr.length;i++){
largeCM*=sortArr[i];
}
/*Array of multiples of last element of SortArray*/
let newArr=[];
for(let i=arr[1];i<=largeCM;i+=arr[1]){
newArr.push(i);
}
/*Finding the final final possible values*/
for(let i=arr[0];i<=arr[1];i++){
for(let j=0;j<newArr.length;j++){
if(newArr[j]%i !=0){
newArr.splice(j,1);
}
}
}
/*Finding the LCM*/
return newArr[0];
}
smallestCommons([1,5]);