From e34ade320d056e509ddfb94d515221fdd03b15e7 Mon Sep 17 00:00:00 2001 From: Amit Jaggernauth Date: Fri, 5 Jun 2026 20:45:28 -0400 Subject: [PATCH] Added LCM and LCM test files --- src/utils/lcm.js | 29 +++++++++++++++++++++++++++++ test/utils/lcm.test.js | 22 ++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/utils/lcm.js create mode 100644 test/utils/lcm.test.js diff --git a/src/utils/lcm.js b/src/utils/lcm.js new file mode 100644 index 0000000..08df8b9 --- /dev/null +++ b/src/utils/lcm.js @@ -0,0 +1,29 @@ +//Name: Amit Jaggernauth +//Professor: Richard Krasso +//Course: WEB-450 +//Date: 6/9/2024 + +// lcm.js + +/** + * Calculate the Greatest Common Divisor (GCD) + * using the Euclidean algorithm. + */ +function gcd(a, b) { + if (a === 0) return b; + return gcd(b % a, a); +} + +/** + * Calculate the Least Common Multiple (LCM) + * of two positive integers. + */ +function lcm(a, b) { + if (a <= 0 || b <= 0) { + throw new Error("Inputs must be positive integers"); + } + + return Math.abs(a * b) / gcd(a, b); +} + +module.exports = { lcm, gcd }; diff --git a/test/utils/lcm.test.js b/test/utils/lcm.test.js new file mode 100644 index 0000000..5910bed --- /dev/null +++ b/test/utils/lcm.test.js @@ -0,0 +1,22 @@ +//Name: Amit Jaggernauth +//Professor: Richard Krasso +//Course: WEB-450 +//Date: 6/9/2024 + +// lcm.test.js + +const { lcm } = require("./lcm"); + +describe("LCM Function", () => { + test("calculates LCM of two small numbers", () => { + expect(lcm(4, 6)).toBe(12); + }); + + test("calculates LCM when one number is a multiple of the other", () => { + expect(lcm(5, 20)).toBe(20); + }); + + test("throws an error for non-positive integers", () => { + expect(() => lcm(-3, 6)).toThrow("Inputs must be positive integers"); + }); +});