Smallest common multple

Smallest common multple
0.0 0

#1

Hi there,

I am struggling hard with this challenge, after cruising through all the previous challenges, the past 3 have been really challenging to the point where I am doubting about what I thought I know…

I tried to simplify this challenge by solving it by pieces. After finding a math formula to find the lcm ( LCM(a,b) = (a*b)/GCD(a,b) ), i succeeded to make it work but only for 2 numbers.

function smallestCommons(arr) {

function remainder(a, b){
  
  if (a === 0){
    return b;
  }

  while (b !== 0){
     if (a > b)
     a = a - b;
     else
     b = b - a;
    }
  
  return a;
  }
  
var cgd = remainder(arr[0], arr[1]);
  
var lcm = (arr[0]*arr[1])/cgd;
  
return lcm;
    
}
console.log(smallestCommons([12, 30]));

After doing that I thought that i will just have to implement a loop somehow to go through the range and pass every number, but I can’t figure how to do so. I keep looking at the solution of other people and can hardly understand them.

I am clearly lacking a great dose of logic on this one and I could use some help on how to get to the next step!

Thank you!


#2

This isn’t your answer, but b = b - a; can be simplified ti b-=a

These challenges are very hard, but the knowledge gained is useful.


#3

Hi John,

Thanks for the tip! yes those are getting harder, even make me question if I will be able to complete all of them! it’s not even the advanced one.

Thanks for your help.


#4

Me and a lot of people i have seen have never done advanced. Intermediate and basic is what is required. I feel as there just not interesting enough to spend 5 hours per on. You could write a decent amount of code for your projects in that time.


#5

If you’re still struggling with this one, here’s a non-elegant way to complete this: you could brute-force it by having a loop check if a particular number is divisible by all the numbers given. If it isn’t, then increment that number by one and try again.


#6

Hi,

Thanks for the reply. I left this one unfinished, as I was losing my mind too much! I think this could be solved using a recursive function. What do you think?


#7

You are way closer than you think. I will drop you the following hint and leave the rest to up to you.
fcc_lcm
You already have the base case of this recursive formula namely the lcm of just two numbers.


#8

Or you could just follow a tutorial: https://www.youtube.com/watch?v=LpGgtkmRvqc

I feel like this challenge was too hard.