-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.js
69 lines (53 loc) · 1.33 KB
/
operations.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function multiply(left, right) {
let l = Math.abs(left), r = Math.abs(right)
let result = 0
for (let i = 0; i < l; i++)
result += r
return onlyOnMinusNumber(left, right) ? -result : result
}
function subtract(left, right) {
return left + negate(right)
}
function divideBrutForce(left, right) {
let l = Math.abs(left), r = Math.abs(right)
let i = 1;
while (l != multiply(i, r))
i++
return onlyOnMinusNumber(left, right) ? -i : i
}
function divide(left, right) {
let l = Math.abs(left), r = Math.abs(right)
let times = 0;
let product = 0
while (l != product) {
product += r
times++
}
return onlyOnMinusNumber(left, right) ? -times : times
}
5
function negate(a) {
let r = 0, negate = 0
let nsign = a < 0 ? 1 : -1
let sign = a > 0 ? 1 : -1
while(r != a)
{
r += sign
negate += nsign
}
return negate
}
function onlyOnMinusNumber(left, right) {
return (left > 0 && right <= 0) || (right > 0 && left <= 0)
}
console.log(subtract(5, 3))
console.log(subtract(3, 5))
console.log(subtract(-3, 5))
console.log(subtract(-3, -5))
console.log(multiply(3, -5))
console.log(multiply(-5, -5))
console.log(divide(9, 3))
console.log(divide(8, 2))
console.log(divide(-8, 2))
console.log(divide(-8, -2))
console.log(negate(-10))