-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
141 lines (112 loc) · 3.96 KB
/
script.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
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
133
134
135
136
137
138
139
140
141
let first_number = "0";
let second_number = "0";
let result = "0";
let current_operator;
let evaluation = [];
const screen = document.querySelector(".screen");
const keyboard = document.querySelector(".keyboard");
const hourEl = document.querySelector('.hour');
const minuteEl = document.querySelector('.minute');
const valueEl = document.querySelector('.value');
keyboard.addEventListener('click', function(e) {
e.stopImmediatePropagation()
onButtonPress(e);
});
function onButtonPress (e) {
switch(e.target.getAttribute('data-button-type')) {
case "digit":
AssignNumber(e)
break;
case "operator":
AssignOperation(e)
break;
}
Render(e);
}
function AssignNumber(e) {
if(evaluation.length <= 1) {
first_number = first_number == "0"
? e.target.getAttribute("data-value")
: first_number + e.target.getAttribute("data-value")
if(evaluation.length == 1) evaluation.shift();
evaluation.push(first_number)
result = first_number;
return;
}
if (evaluation.length >= 2) {
second_number = second_number == "0"
? e.target.getAttribute("data-value")
: second_number + e.target.getAttribute("data-value");
if(evaluation.length == 3) evaluation.pop();
evaluation.push(second_number);
result = second_number;
}
}
function AssignOperation(e) {
current_operator = e.target.getAttribute('data-value');
// Exclusive operations that can be performed with one number, in the case of clear it can be executed even when the evaluation array is empty
if(current_operator == "%" || current_operator == "+/-" || current_operator == "clear" || current_operator == "=") return Operate();
if(evaluation.length == 3) Operate();
if(evaluation.length == 2) evaluation.pop();
evaluation.splice(1, 1, current_operator);
}
function Operate() {
if(current_operator == "%" && evaluation.length) {
let number = parseInt(evaluation[evaluation.length - 1])
result = (number / 100).toString();
evaluation.splice(evaluation.length - 1, 1, result);
return;
}
if(current_operator == "+/-" && evaluation.length) {
result = (evaluation[evaluation.length - 1] * -1).toString();
evaluation.splice(evaluation.length - 1, 1, result);
return;
}
if(current_operator == "clear") {
if(evaluation.length <= 2) {
first_number = "0";
evaluation = [];
result = "0";
return;
}
if(evaluation.length == 3) {
second_number = "0";
evaluation = [first_number.toString()]
result = first_number.toString();
return;
}
}
if(evaluation.length == 3) {
result = (eval(evaluation.join().replace(/,/g, ""))).toString();
first_number = result;
second_number = "0";
evaluation = [first_number]
}
}
function Render(e) {
const clear_button = document.querySelector('div[data-value="clear"]');
let new_operator_button = e.target;
let last_operator_button = document.querySelector('.selected_operation');
last_operator_button ? last_operator_button.classList.remove('selected_operation') : null;
new_operator_button ? new_operator_button.classList.add('selected_operation') : null;
// change screen's font-size
switch(result.toString().length) {
case 7:
screen.style.fontSize = "4.7rem"
break;
case 8:
screen.style.fontSize = "4.1rem"
break;
case 9:
screen.style.fontSize = "3.65rem"
break
}
if(result.toString().length > 9) {
screen.textContent = parseFloat(result).toPrecision(3);
} else {
screen.textContent = result;
}
evaluation.length == "0"
? clear_button.textContent = 'AC'
: clear_button.textContent = 'C'
}