Intermediate Javascript - Smallest Common Multiple

i m trying to solve this problem i checked google and tried with this formula

{\displaystyle \operatorname {lcm} (a,b)={\frac {|ab|}{\gcd(a,b)}}.}{isplaystyle peratorname {lcm} (a,b)={rac {|ab|}{cd(a,b)}}.}
and i’m trying with recursion actually i m finding the smallest common multiple but this last has no relation with other common multiples so i can try if they are divible by the number in the range that s why i’m thinking about recursion here s my code

function gcd(arr)
{ 
    do
    {
if (arr[0]>arr[1])
{
    arr[0] = arr[0] % arr[1];
}
if (arr[0]<arr[1])
{
    arr[1] = arr[1] % arr[0];
}
    }
while ((arr[0]>0) && (arr[1]>0))
if (arr[0]>arr[1])
return arr[0]
if (arr[1]>arr[0])
return arr[1]
}
console.log(gcd([1,5]))








function lcm(arr)
{ let lcd=Math.abs(arr[0]*arr[1])/gcd(arr) ;console.log(lcd)
do 
    { let j=arr[0]
        if ((lcd % j !==0))
        {
            lcm(arr)
        }
    }
    while(arr[0]<arr[1])
    
   return lcd
}
console.log(lcm([1,5]))

Formatting for human readability:

function gcd(arr) {
  do {
    if (arr[0] > arr[1]) {
      arr[0] = arr[0] % arr[1];
    }
    if (arr[0] < arr[1]) {
      arr[1] = arr[1] % arr[0];
    }
  } while ((arr[0] > 0) && (arr[1] > 0))
  if (arr[0] > arr[1]) return arr[0]
  if (arr[1] > arr[0]) return arr[1]
}
console.log(gcd([1, 5]))

function lcm(arr) {
  let lcd = Math.abs(arr[0] * arr[1]) / gcd(arr);
  console.log(lcd);
  do {
    let j = arr[0]
    if ((lcd % j !== 0)) {
      lcm(arr)
    }
  } while (arr[0] < arr[1])

  return lcd
}
console.log(lcm([1, 5]))

It looks like you are getting mixed up with what is an array and what is a scalar. I would rework your functions gcd and lcm to use a pair of input.

Also, you deleted the function smallestCommons but that function is mandatory to pass this challenge.

i m implementing my solution inside lcm because i’m trying on vs code before passing to the freecodecamp tests .concerning the difference between scalars an array ,the truth i didn t hear about scalars before it seems they are a particular type of arrays no?

A scalar is just a single number. It isn’t a type of array.

My point here is that your are swapping back and forth between arrays and single numbers. Just pick one. Take two numbers as an input and return one number as an output for the lcm and gcd functions.

to be honest i didn’t understand what you mean exactly nevertheless i reformulated my code into one function like i’ m supposed to do in the challenge i’m searching for the gcd to find the lcm according to the Euclid algorithm and the formula i mentionned above
lcm(a,b)=|a*b|/gcd(a,b) ,actually i don t have a problem with finding the lcm but my problem is to check if it s divisible by all numbers in the range of the array for example smallestCommon([1,5]) needs to be evenly divisible by 1,2,3,4,5 after finding the lcm ,thus my problem is how to come back to a greater multiple if it s not divisble by numbers in the range of the given array
here s my reformulated code

{let gcd=0
 let lcd=0
   do 
   {
      if(arr[0]>arr[1])
      {
        arr[0]=arr[0]%arr[1]
      }
      if(arr[1]>arr[0])
      {
        arr[1]=arr[1]%arr[0]
      }
   } while((arr[0]>0) && (arr[1] > 0))
             if(arr[0]>arr[1])
             gcd=arr[0]
             if(arr[1]>arr[0])
             gcd=arr[1]
lcd=Math.abs(arr[0]*arr[1])/gcd
                 for (let i=arr[0];i<=arr[1];i++)
                 {
                    if(lcd % i ==0)
                    return lcd
                    else
                    {
   /*problem here how to come back to a greater
 common multiple if not evenly divisible
 by all numbers in the range of the array*/
                    }
                 } 
}
console.log(smallestCommon([1,5]))

You are using arrays in your computation of LCM and GCD. Your code is very confusing to follow because of that. Use single values, like a and b instead.

Again, I have formatted your code so that humans can read it. It is somewhat rude to post extremely poorly formatted code when you are asking for help:

{
  let gcd = 0;
  let lcd = 0;

  do {
    if (arr[0] > arr[1]) {
      arr[0] = arr[0] % arr[1];
    }
    if (arr[1] > arr[0]) {
      arr[1] = arr[1] % arr[0];
    }
  } while ((arr[0] > 0) && (arr[1] > 0))
  if (arr[0] > arr[1]) gcd = arr[0];
  if (arr[1] > arr[0]) gcd = arr[1];
  lcd = Math.abs(arr[0] * arr[1]) / gcd;
  for (let i = arr[0]; i <= arr[1]; i++) {
    if (lcd % i == 0) return lcd;
    else {
      /*problem here how to come back to a greater
    common multiple if not evenly divisible
    by all numbers in the range of the array*/
    }
  }
}
console.log(smallestCommon([1, 5]))

I would go back to having separate GCD and LCM functions. I did not recommend that you remove those functions, only that you add back the main function that is required.

I’m really sorry for that i don’t mean it but i was writing just 3 back ticks after i close with 3 others according to this link Markdown Code Formatting i need to press [shift +enter] i’ m not doing it for a purpose please if it s not the right way correct me again here s my code

function gcd(arr) {
   var a=arr[0];
   var b=arr[1]
    do {
      if (a > b) {
        a = a % b;
      }
      if (a < b) {
        b = b % a;
      }
    } while ((a > 0) && (b > 0))
    if (a > b) return a
    if (b > a) return b
  }
  console.log(gcd([1, 5]))
  
  function lcm(arr) {
    let x=arr[0]
    let y=arr[1]
    let lcd = Math.abs(x * y) / gcd(arr);
    console.log(lcd);
    
    return lcd
  }
  function smallestCommon(arr)
  {let i=arr[0]
    let j=arr[1]
    for (i;i<=j;i++)
                 {
                    if(lcm(arr) % i ==0)
                    return lcm(arr)
                    else
                    {
   /*problem here how to come back to a greater
 common multiple if not evenly divisible
 by all numbers in the range of the array*/
                    }
                }   
  }
  console.log(lcm([1, 5]))
  console.log(smallestCommon([1,5]))

i wish it s formatted now :cold_face: :disappointed_relieved:

You’re still missing what I am saying. Use zero arrays here.

Use zero arrays here.

Here, I’m not sure what you are trying to do, but Wikipedia has a formula for what you need to do.

Side note - Don’t use var, it is a legacy feature.

Do-while loops are pretty uncommon. I’d make sure you really, really need a do-while.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.