-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
132 lines (103 loc) · 3.37 KB
/
script.js
File metadata and controls
132 lines (103 loc) · 3.37 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
let count = []
let saveAction
const MAX_VISOR_CHAR = 10
function AddNumber(num) {
document.getElementById("total").removeAttribute("hidden")
if (document.getElementById("total").innerHTML.length < MAX_VISOR_CHAR) {
document.getElementById("total").innerHTML += num
}
}
function CalcAction(action) {
var currentNumber = document.getElementById("total").innerHTML
if (currentNumber.length === 0) { return }
count.push(Number(document.getElementById("total").innerHTML))
if (currentNumber.split('')[currentNumber.length - 1] == '.') {
document.getElementById("accumulator").removeAttribute("hidden")
document.getElementById("accumulator").innerHTML += ` ${document.getElementById("total").innerHTML}0 ${action}`
}
else {
document.getElementById("accumulator").removeAttribute("hidden")
document.getElementById("accumulator").innerHTML += ` ${document.getElementById("total").innerHTML} ${action}`
}
document.getElementById("total").innerHTML = ""
count.push(action)
}
function AddComma() {
var currentNumber = document.getElementById("total").innerHTML
if (currentNumber == '') {
document.getElementById("total").removeAttribute("hidden")
document.getElementById("total").innerHTML = "0."
}
else if (!currentNumber.includes(".")) {
document.getElementById("total").innerHTML += "."
}
}
function Result() {
currentAccum = document.getElementById("accumulator").innerHTML
currentNumber = document.getElementById("total").innerHTML
if (currentAccum[currentAccum.length - 1] === "=" && currentNumber.length > 0) {
document.getElementById("total").innerHTML = ProcessAction(Number(currentNumber), Number(currentNumber), saveAction).toString().substring(0, MAX_VISOR_CHAR)
}
if (count.length === 0) { return }
count.push(Number(document.getElementById("total").innerHTML))
document.getElementById("accumulator").innerHTML += ` ${document.getElementById("total").innerHTML} =`
ProccessResult()
}
function ProccessResult() {
let action = null
let current = null
let total = 0;
if (isNaN(count[count.length - 1])) {
count.pop()
}
count.forEach(n => {
if (!isNaN(n)) {
if (current == null) {
current = n
} else {
total += ProcessAction(current, n, action)
current = null
}
} else {
action = n
saveAction = n
}
})
if (current != null) {
total = ProcessAction(total, current, action)
}
document.getElementById("total").innerHTML = total.toString().substring(0, MAX_VISOR_CHAR)
count = []
}
function ProcessAction(num1, num2, action) {
switch (action) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case 'x':
return num1 * num2;
case '/':
if (num2 === 0) {
alert("Division by zero is not allowed")
throw new Error("Error");
}
return num1 / num2;
default:
throw new Error("Invalid operation: " + action);
}
}
function CleanCurrentEntry() {
document.getElementById("total").innerHTML = ""
}
function CleanAll() {
document.getElementById("total").innerHTML = ""
document.getElementById("accumulator").innerHTML = ""
count = []
}
function Percentage() {
var currentNumber = document.getElementById("total").innerHTML
if (currentNumber != "") {
document.getElementById("total").innerHTML = Number(document.getElementById("total").innerHTML) / 100
}
}