Back to Freecodecamp

Challenge 103: LCM

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/68ffb91507a5b645769328c6.md

latest1.0 KB
Original Source

--description--

Given two integers, return the least common multiple (LCM) of the two numbers.

The LCM of two numbers is the smallest positive integer that is a multiple of both numbers. For example, given 4 and 6, return 12 because:

  • Multiples of 4 are 4, 8, 12 and so on.
  • Multiples of 6 are 6, 12, 18 and so on.
  • 12 is the smallest number that is a multiple of both.

--hints--

lcm(4, 6) should return 12.

js
assert.equal(lcm(4, 6), 12);

lcm(9, 6) should return 18.

js
assert.equal(lcm(9, 6), 18);

lcm(10, 100) should return 100.

js
assert.equal(lcm(10, 100), 100);

lcm(13, 17) should return 221.

js
assert.equal(lcm(13, 17), 221);

lcm(45, 70) should return 630.

js
assert.equal(lcm(45, 70), 630);

--seed--

--seed-contents--

js
function lcm(a, b) {

  return a;
}

--solutions--

js
function lcm(a, b) {
  function gcd(x, y) {
    return y === 0 ? x : gcd(y, x % y);
  }

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