MY #100 DAYS OF CODE CHALLENGE JOURNEY-DAY 2
Problem: Finding Smallest Common Multiple in the range of two numbers e.g. 4 and 8. This implies finding the smallest common multiple in the range of 4 up to 8.
Problem Definition: smallest common multiple is a number which is the lowest number which is a multiple of the range of numbers and common to all the numbers i.e. all the range of numbers have it in common.
Algorithm
I sorted out all the numbers from from highest to lowest and created a list for them.
The product of the first two greatest numbers is likely to be the smallest common multiples and all the numbers in the range are being tested if they can divide this product without a remainder .
If the above step return true, this will be the smallest common multiple else I test for the product multiple and divide all the range of numbers through it until I get the smallest common multiple.
I then return the result.
JavaScript Code
function smallestComonMultiple(arr) {
arr.sort(function(a,b){
return b-a;
});
let newArr = [];
for(i = arr[0]; i>=arr[1];i--){
newArr.push(i);
}
let quot = 0;
let loop = 1;
let n;
do {
quot = newArr[0] * loop * newArr[1] ;
for( n = 2; n < newArr.length; n++ ){
if(quot%newArr[n] !== 0){
break;
}
}
loop++
}
while(n!==newArr.length)
return quot;
}
console.log(smallestComonMultiple([8,4]));
https://carbon.now.sh/