function smallestCommons(arr) {
arr= arr.sort((a,b)=>a-b);
let a=arr[0];
let b=arr[1];
let k=[];
let d=b;
let z=0;
while(z!=arr.length)
{z=0;
for(var i=a;i<=b;i++)
{
if(d%i==0)
z++;
}
d++;
}
console.log(z);
return d;
}
console.log(smallestCommons([1,5]));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.
// find LCM for two numbers
function lowestCommonMultiple(x, y) {
for (let i = x; i <= x * y; i += x) { //generate list of multiples of x
for (let j = y; j <= x * y; j += y) { // generate list of multiples of y
if (i === j) { // find the match
return i;
}
}
}
}
/*
In the for loops the stopping condition is just the multiple of the two numbers,
since that is the last resort case.
I couldn’t see how else to define the end of the loop.
*/
// create array of all numbers from the lower to the higher of input array
function setRange(x, y) {
if (y < x) { // this block corrects for reversed order in input array
const newX = y;
const newY = x;
x = newX;
y = newY;
}
let array = ;
for (let i = x; i <= y; i++) {
array.push(i);
}
return array;
}
// run through array finding LCM of successive pairs
function arrayLowestCommon(array) {
let common = array[0];
for (let i = 0; i < array.length - 1; i++) {
common = lowestCommonMultiple(common, array[i + 1]);
}
return common;
}
const first = arr[0];
const last = arr[1];
const range = setRange(first, last);
const result = arrayLowestCommon(range);
return result;
}