Find the LCM of two numbers in javascript - LearnersBucket

PHOTO EMBED

Fri Dec 03 2021 23:56:02 GMT+0000 (Coordinated Universal Time)

Saved by @tolanisirius

let lcm = (n1, n2) => {
  //Find the smallest and biggest number from both the numbers
  let lar = Math.max(n1, n2);
  let small = Math.min(n1, n2);
  
  //Loop till you find a number by adding the largest number which is divisble by the smallest number
  let i = lar;
  while(i % small !== 0){
    i += lar;
  }
  
  //return the number
  return i;
}
content_copyCOPY

https://learnersbucket.com/examples/algorithms/find-the-lcm-of-two-numbers-in-javascript/