Hi all. I just want to know if I’m on the right track with my code or going all wrong about it? It’s been ages since I had any math/calculus so these assignments are extra challenging.
So the assignment is : smallest common multiple , of intermediate javascript.
function smallestCommons(arr) {
let newArr=[];
if(arr[0]<arr[1]){
for(let i=arr[0];i<=arr[1];++i){
newArr.push(i);
}
}else {
for(let i=arr[0];i>=arr[1];--i){
newArr.push(i);
}
}
//first sort from smallest to largest.
newArr.sort(function(a, b) {
return a - b;
});
//find common multiples, limit, upper largest arr[i] is max.
let multiArr=[]; let product=0;
let max = Math.max(...newArr);
for(let i=1;i<newArr.length;++i){
for(let multiplier=1;multiplier<=max;++multiplier){
product = newArr[i]*multiplier;
multiArr.push(product);
}
}
let leastMultiple=[];
//use remainder operator (%) to check if the reminder of a division is 0, which means it is evenly divisible
for(let i=0;i<newArr.length;++i){
for(let j=0;j<multiArr.length;++j){
if(newArr[i]%multiArr[j] ===0){
leastMultiple.push(multiArr[j]);
}
}
}
//at this point remove doubles from leastMultiple and multiply them for final answer?
return leastMultiple;
}