 # Need help with smallest common multiple challenge

I am currently doing the smallest common multiple problem and my code works but the problem is that it takes so long to get an answer that it gives me a fail when I put it into freecodecamp. So I was wondering if there was anyway to make my code more effective.

Start of Code:

``````function largerNum(arr){
if(arr > arr){
return arr;
}else if(arr < arr){
return arr;
}
}

function smallerNum(arr){
if(arr < arr){
return arr;
}else if(arr > arr){
return arr;
}
}

function loopThrough(start,end,number){
while(start <= end){
if(number%start !== 0){
return false;
}
start++;
}
return true;
}

function smallestCommons(arr) {
var arr1 = [];
var arr2 = [];
var temp1;
var temp2;
var testCase = true;
var largeNum = largerNum(arr);
var smallNum = smallerNum(arr);
var i = 1;
while(testCase){
temp1 = i * arr;
temp2 = i * arr;

arr1.push(temp1);
arr2.push(temp2);

if(arr1.indexOf(temp2) > -1 && arr2.indexOf(temp2) > -1 && loopThrough(smallNum,largeNum,temp2)){
testCase = false;
return temp2;
}else if(arr1.indexOf(temp1) > -1 && arr2.indexOf(temp1) > -1 && loopThrough(smallNum,largeNum,temp1)){
testCase = false;
return temp1;
}
i++;
}
}

console.log(smallestCommons([23,18]));``````

This challenge can be solved efficiently enough to pass without turning the loop protection off, but it might take you some finagling and probably research into the math.

As a hint, my solution has 3 functions: smallestCommons, a least common multiple function, and a greatest common denominator function.

Thank you for the response. Turning off loop protection made my code work in freecodecamp, but I am still interested in figuring out how to make my code better. What I think I am confused about is how will finding the GCD help solve the problem because I can see how to make the function using the Euclidean Algorithm I just don’t know how to implement it.

I’m going to hazard a guess that what you are missing is the knowledge that a function can call itself. Does that make implementing the Euclidean Algorithm make more sense?

If not, and since you have solved it, you don’t need to feel too bad about studying more efficient ways on the GitHub freecodecamp wiki. Thank you! I didn’t realize that there are examples of how to solve the challenges available. I will definitely go back and look through these.

You can also look at other campers’ solutions on their profiles. Some users have their profiles set to private, but most of us don’t.

I’m not saying this is the best solution, but here is how I got it to work without turning loop protect off.

Spoiler
``````
function smallestCommons(arr) {
var smallest = Math.min(arr,arr);
var biggest = Math.max(arr,arr);
var least = lcm(smallest, smallest+1);
if (biggest - smallest === 1){
return least;
} else {
return lcm(least, smallestCommons([smallest+1, biggest]));
}
}

function lcm(a,b) {
return Math.abs(a*b)/gcd(a,b);
}

function gcd(a,b) {
if (b === 0){
return a;
} else {
a = a-b*(Math.floor(a/b));
return gcd(b,a);
}
}

``````