diff --git a/Maths/IsDivisible.ts b/Maths/IsDivisible.ts new file mode 100644 index 00000000..bed87628 --- /dev/null +++ b/Maths/IsDivisible.ts @@ -0,0 +1,16 @@ +/** + * @function IsDivisible + * @description Checks is number is divisible by another number without remainder. + * @param {number} num1 - first number, a dividend. + * @param {number} num2 - second number, a divisor. + * @return {boolean} - true if first number can be divided by second number without a remainder. + * @example IsDivisible(10, 2) = true + * @example IsDivisible(11, 3) = false + */ + +export const IsDivisible = (num1: number, num2: number): boolean => { + if (num2 === 0) { + throw new Error('Cannot divide by 0'); + } + return num1 % num2 === 0; +}; diff --git a/Maths/test/IsDivisible.test.ts b/Maths/test/IsDivisible.test.ts new file mode 100644 index 00000000..f671f980 --- /dev/null +++ b/Maths/test/IsDivisible.test.ts @@ -0,0 +1,41 @@ +import { IsDivisible } from "../IsDivisible"; + +describe("IsDivisible", () => { + test.each([ + [1, 1], + [6, 3], + [101, 1], + [5555, 5], + [143, 13], + [535, 107], + [855144, 999], + [100000, 10], + [1.5, 0.5] + ])( + "%f is divisible by %f", + (num1, num2) => { + expect(IsDivisible(num1, num2)).toBe(true); + }, + ); + + test.each([ + [1, 2], + [61, 3], + [120, 11], + [5556, 5], + [10, 9], + [75623, 3], + [45213, 11], + [784, 24], + [1.2, 0.35] + ])( + "%f is not divisible by %f", + (num1, num2) => { + expect(IsDivisible(num1, num2)).toBe(false); + }, + ); + + test("should not divide by 0", () => { + expect(() => IsDivisible(10, 0)).toThrow(); + }); +}); \ No newline at end of file