-
Notifications
You must be signed in to change notification settings - Fork 0
/
elementary.js
50 lines (36 loc) · 943 Bytes
/
elementary.js
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
function multiply(a , b) {
let newb = Math.abs(b)
let newa = Math.abs(a)
let res = 0
for (let index = 0 ; index < newb ; index++) {
res += newa
}
if (a < 0 && b < 0) return res
if ((a < 0) || (b < 0)) return -res
return res
}
function divide(a , b) {
if (b === 0) return new Error("Cant divide by 0")
let newa = Math.abs(a)
let newb = Math.abs(b)
let res = 0
while (newa > 0 && newa >= newb) {
newa = newa - newb
res ++
}
if (a < 0 && b < 0) return res
if ((a < 0) || (b < 0)) return -res
return res
}
function modulo(a , b) {
if (b === 0) return new Error("Cant divide by 0")
let newa = Math.abs(a)
let newb = Math.abs(b)
let res = 0
while (newa > 0 && newa >= newb) {
newa = newa - newb
res ++
}
if (a < 0) return -newa
return newa
}