File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments