Skip to content

algorithm: is divisible #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Maths/IsDivisible.ts
Original file line number Diff line number Diff line change
@@ -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;
};
41 changes: 41 additions & 0 deletions Maths/test/IsDivisible.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});