Skip to content

Commit dd4aa5a

Browse files
authored
feat: add IsDivisible function (#43)
1 parent 1d655b4 commit dd4aa5a

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Maths/IsDivisible.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @function IsDivisible
3+
* @description Checks is number is divisible by another number without remainder.
4+
* @param {number} num1 - first number, a dividend.
5+
* @param {number} num2 - second number, a divisor.
6+
* @return {boolean} - true if first number can be divided by second number without a remainder.
7+
* @example IsDivisible(10, 2) = true
8+
* @example IsDivisible(11, 3) = false
9+
*/
10+
11+
export const IsDivisible = (num1: number, num2: number): boolean => {
12+
if (num2 === 0) {
13+
throw new Error('Cannot divide by 0');
14+
}
15+
return num1 % num2 === 0;
16+
};

Maths/test/IsDivisible.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { IsDivisible } from "../IsDivisible";
2+
3+
describe("IsDivisible", () => {
4+
test.each([
5+
[1, 1],
6+
[6, 3],
7+
[101, 1],
8+
[5555, 5],
9+
[143, 13],
10+
[535, 107],
11+
[855144, 999],
12+
[100000, 10],
13+
[1.5, 0.5]
14+
])(
15+
"%f is divisible by %f",
16+
(num1, num2) => {
17+
expect(IsDivisible(num1, num2)).toBe(true);
18+
},
19+
);
20+
21+
test.each([
22+
[1, 2],
23+
[61, 3],
24+
[120, 11],
25+
[5556, 5],
26+
[10, 9],
27+
[75623, 3],
28+
[45213, 11],
29+
[784, 24],
30+
[1.2, 0.35]
31+
])(
32+
"%f is not divisible by %f",
33+
(num1, num2) => {
34+
expect(IsDivisible(num1, num2)).toBe(false);
35+
},
36+
);
37+
38+
test("should not divide by 0", () => {
39+
expect(() => IsDivisible(10, 0)).toThrow();
40+
});
41+
});

0 commit comments

Comments
 (0)