-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalcolatrice.js
60 lines (50 loc) · 1.2 KB
/
calcolatrice.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
to_eval = ""
is_result = false
function get_calcolatrice_output() {
output = document.getElementById("calcolatrice_output");
}
function add_value(num) {
if (is_result) {
output.value = ""
is_result = false
}
if ((output.value+num).length >= 13)
return;
output.value += num
}
function add_decimal_point() {
if (!output.value.includes("."))
output.value += "."
}
function remove_leading_zero(num) {
return num*1;
}
function add_operator(op) {
if (output.value === "") {
if (to_eval === "")
return;
to_eval = to_eval.substring(0, to_eval.length-1)
}
to_eval += remove_leading_zero(output.value) + op
output.value = ""
}
function calculate() {
if (to_eval === "")
return
if (output.value === "")
to_eval = to_eval.substring(0, to_eval.length-1)
result = eval(to_eval + output.value)
if (result.toString().length >= 13) {
result = result.toExponential(7);
}
output.value = result
to_eval = ""
is_result = true
}
function del() {
output.value = output.value.substring(0, output.value.length - 1)
}
function reset() {
to_eval = ""
output.value = ""
}