-
Notifications
You must be signed in to change notification settings - Fork 0
/
cut-corners.js
51 lines (44 loc) · 947 Bytes
/
cut-corners.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
function isNegative(nb) {
return nb < 0;
}
function GetIntPart(nb) {
let intPart = 0;
if (nb >= 0) {
while (intPart + 1 <= nb) {
intPart++;
}
} else {
while (intPart - 1 >= nb) {
intPart--;
}
}
return intPart;
}
function round(nb) {
const Myint = GetIntPart(nb);
const Fawasil = nb - Myint;
if (Fawasil > 0.5 || (Fawasil === 0.5 && !isNegative(nb))) {
return Myint + 1;
} else if (Fawasil < -0.5 || (Fawasil === -0.5 && isNegative(nb))) {
return Myint - 1;
}
return Myint;
}
function ceil(nb) {
const Myint = GetIntPart(nb)
if (nb > Myint) {
return Myint + 1;
}
return Myint;
}
function floor(nb) {
const Myint = GetIntPart(nb)
if (nb < Myint) {
return Myint - 1;
}
return Myint;
}
function trunc(nb) {
return GetIntPart(nb);
}
console.log(round(45555.55));