# Smallest Common Multiple help me with my trash code 8D

Tell us what’s happening:
my code is logically sound, but I suppose making my code loop 6 million times is a little too much xD
So I don’t know what to do anymore, I’ve been doing this for hours and finally got it right but it was “too much” load and so FCC isn’t accepting it (and I kind of agree) basically here’s how it should be

I need to return a range from the given array
if 5, 1 then 5 4 3 2 1 or 1 2 3 4 5
then find the least common multiple which in 5, 1 is 60 since 60 is the lowest number divisible byall 1 2 3 4 5 without remainders

My approach
my approach was basically increment a variable until all of it is divisible by all the numbers in an array. and my computer didn’t find it fun and was having trouble looping through something 6 million times.

so now, how do I redo this thing? xD I’m totally lost.

``````

function smallestCommons(arr) {
let newArr = [...arr]
newArr.sort(function(a, b) {
return b -a;
});
let range = [];
for (let i = newArr[0]; i >= newArr[1]; i--){
range.push(i);
}
console.log(range + " after first for loop");

let flag = true;
let x = 0;

while (flag){
x++
for (let i = 0; i < range.length; i++){
if (x % range[i] != 0){
break;
} else if (i == [range.length -1]){
flag = false;
}

}
}
return x;
}
smallestCommons([2, 10]);

/*
*/
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36`.

If you poke around on the internet (*cough* Wikipedia *cough*) you’ll find that there is a mathematical foruma for finding the LCD. You would need to adapt it to code and there’s additional work you need to do to achieve the actual goal that FCC is asking for, but it would be a good place to start.

and there I was, led to believe that you don’t need algebra in “real life”. I just saw the term Euclid’s algorithm. I say Euclid had too much time! xD Okay I’ll see what I can do with this, thanks for the tip!

Calculus may not come up much (unless you happen to work in a field where you have to model the phyiscal world…like programming), but strong algebra skills are seriously underated.

Where were you in my youth?! xD Is it alright if I just googled “javascript euclidean lcm algorithm”? I mean, that’s how you do it at work right? Don’t get me wrong, I agree with your assessment with algebra, but I’ve been working on this thing for at least 4 hours xD

Take a break. Have a snack. Pet a dog or see some sunshine. Hydrate. Come back fresh. Turning an algorithm or solution that is not written in the language that you code in into a coded solution is a significant part of how I actually do it at work.

Hi you two,
I had a look at the question and think your suggestion of the least commen denominator might not fit the question.
The anser should have no remains by not only dividing through the two given numers but also by dividing through the range of them…
My clumsy attempt would be to split all elements of the range in their prime numbers and multiply the result only with the primes I have not jet added / multiplied to my answer. - the only problem is that my result is than better then the given ansers in the question. e.g. 1,13 -> my answer 90 090 instead of 360360. (4x my answer)…

I’m not sure about your approach, but my approach does divide through the whole range, that’s why it’s looping 6 million times and so it’s incredibly in efficient. I actually get the right answer but FCC doesn’t accept it because it’s looping too many times.

I think I’m going to sleep or else I, through no fault of my own, shall have to stab the next human that talks to me xD Thanks for the insight, I’ll get back to this when my sanity returns.

- that is sometimes the best approach!
I just rerun my numbers and found my mistake … a mangeled the numbers.

To your answer - you are using a bit of a big gun - since you are testing EVERY number until you find one that fits your parameters - the more intelligent approach is to find the number by computation - one way is the one that I suggested.
If you want I could elaborate.

Nah it’s alright, I saw a youtube video that approached it by increasing by the highest number instead of +1, which is actually ingenious if you think about it. Now it made me think if I even really tried to look at this problem logically or I just saw math and immediately went to 1 + 1 xD