-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoduloUsingRecursion.js
More file actions
44 lines (42 loc) · 909 Bytes
/
moduloUsingRecursion.js
File metadata and controls
44 lines (42 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function mod(a, b) {
// let remainder=a
// while (a - b >= 0) {
// remainder = a - b
// a = remainder
// }
// return remainder
if (a<b){
return a
}
return mod(a-b, b)
}
function moduloUsingRecursion(a, b) {
if (b !== 0 && a !== 0) {
if (b < 0 && a > 0) {
b = -1 * b
let y = mod(a, b)
return - 1 * y
}
else if (a < 0 && b > 0) {
a = -1 * a
let y = mod(a, b)
return - 1 * y
}
else if (a < 0 && b < 0) {
a = -1 * a
b = -1 * b
let y = mod(a, b)
return y
}
else if (a > 0 && b > 0) {
let y = mod(a, b)
return y
}
}
else if ((a === 0 && b === 0) || (a !== 0 && b === 0)) {
return -1
}
else {
return 0
}
}