# Smallest common multiple , is this correct

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;
}
``````

No Calculus needed to solve this challenge. Just a basic understanding of what a Prime number is.

If you are going to sort the array anyway, then there is no need for the for loops. Simply create a copy of the original array (think slice) and then sort the new array. You then know for sure that the new array’s first element is the min value and the second element is the max value. This will allow you to change the following line also:

``````let max = Math.max(...newArr);
``````

For the rest of your code, I suggest trying to write out an algorithm on paper first without using code. You need to make sure you understand what steps are necessary. You are adding quite a bit of extra iteration here that is not really needed.

Thank you for your reply. i solved it , part mine code , part gcd/lcm code copied, but it works, and is standard code I’m sure.