-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelementary.js
More file actions
77 lines (73 loc) · 1.63 KB
/
Copy pathelementary.js
File metadata and controls
77 lines (73 loc) · 1.63 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function multiply(a, b) {
let res = 0;
if (b > 0) {
for (let i = 1; i <= b; i++) {
res += a;
}
}
if (b < 0) {
for (let i = -1; i >= b; i--) {
res -= a;
}
}
return res;
}
function divide(a, b) {
let dividande = 0;
let temoin = b;
if (a < 0 || b < 0) {
let signA = Math.sign(a);
let signB = Math.sign(b);
a = Math.abs(a);
b = Math.abs(b);
temoin = b
while (temoin <= a) {
temoin = temoin + b;
dividande++;
}
let sign = multiply(signA, signB);
let res = multiply(dividande, sign)
return res;
}
while (temoin <= a) {
temoin = temoin + b;
dividande++;
}
return dividande;
}
function modulo(a, b) {
let temoin = b;
if (a < 0 || b < 0) {
let B = b;
let A = a;
a = Math.abs(a);
b = Math.abs(b);
temoin = b;
while (temoin <= a) {
temoin = temoin + b;
}
if (temoin > a) {
temoin = temoin - b
}
let res = temoin - a;
if (B < 0 && A > 0) {
let mod = multiply(res, -1)
return mod;
} else {
return res;
}
} else {
let rest = 0;
let temoin = b;
while (temoin <= a) {
temoin = temoin + b;
}
if (temoin > a) {
temoin = temoin - b
}
rest = a - temoin;
return rest;
}
}
let res = modulo(-123, -22);
console.log(res);