-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
197 lines (175 loc) · 6.16 KB
/
main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
let btn = document.querySelectorAll(
'.container__button:not(.container__button--sumup)'
);
//score represents the whole equation already typed to the calculator. The equation is composed of numbers and opeartors (+-*/) between them.
let score = document.querySelector('.container__score');
//typedNumber is a number currently being typed to the calculator
//If user uses an operator (+ - etc.) typedNumber becomes an empty string to be able to get digits of the next number in the equation.
let typedNumber = '';
//Auxiliary variable used in numerous tests in functions below
let lastCharacter = '';
document.addEventListener('keydown', addToEquationKeyDown);
function addToEquationKeyDown(e) {
//Do nothing if the first digit in number is to be zero or a user started number from a fraction and then tries to use an operator
if (
(typedNumber === '' && e.key === '0') ||
(score.textContent === '0.' && /[^0-9]/.test(e.key))
) {
}
//Remove "0." from the equation if an operator is used i.e. 56+0. => 56+
else if (
/[*\/+-]/.test(e.key) &&
/[*\/+-]/.test(score.textContent[score.textContent.length - 3]) &&
score.textContent.slice(
score.textContent.length - 2,
score.textContent.length
) === '0.'
) {
score.textContent =
score.textContent.slice(0, score.textContent.length - 3) + e.key;
typedNumber = '';
lastCharacter = e.key;
}
//Change operator in equation
else if (/[*\/+-]/.test(e.key) && /[*\/+-]/.test(lastCharacter)) {
score.textContent =
score.textContent.slice(0, score.textContent.length - 1) + e.key;
typedNumber = '';
lastCharacter = e.key;
}
//If a number ends with "." and user wants to use an operator the "." should be replaced by the operator
else if (
/[*\/+-]/.test(e.key) &&
/[.,]/.test(score.textContent[score.textContent.length - 1])
) {
score.textContent =
score.textContent.slice(0, score.textContent.length - 1) + e.key;
typedNumber = '';
lastCharacter = e.key;
}
//Adding a digit to the equation. F prevents from getting functional keys (F1,F2 etc.)
else if (/[0-9]/.test(e.key) && !/F/.test(e.key)) {
score.textContent += e.key;
typedNumber += e.key;
lastCharacter = e.key;
//Adding decimal operator to the number
} else if (/[.,]/.test(e.key) && !typedNumber.includes('.')) {
//Add "." if there are already digits in the number
if (typedNumber !== '') {
score.textContent += '.';
typedNumber += '.';
//Create 0 and then add "." if there is no number yet
} else {
score.textContent += '0.';
typedNumber += '0.';
}
lastCharacter = e.key;
//Add an operator to the equation
} else if (/[*\/+-]/.test(e.key) && (typedNumber || score.textContent)) {
score.textContent += e.key;
typedNumber = '';
lastCharacter = e.key;
}
}
btn.forEach((e) => e.addEventListener('click', addToEquationClick));
function addToEquationClick(e) {
//Do nothing if the first digit in number is to be zero or a user started number from a fraction and then tries to use an operator
if (
(typedNumber === '' &&
(e.target.textContent === '0' || e.target.textContent === '00')) ||
(score.textContent === '0.' && /[^0-9]/.test(e.target.textContent))
) {
}
//Remove "0." from the equation if an operator is used i.e. 56+0. => 56+
else if (
/[*\/+-]/.test(e.target.textContent) &&
/[*\/+-]/.test(score.textContent[score.textContent.length - 3]) &&
score.textContent.slice(
score.textContent.length - 2,
score.textContent.length
) === '0.'
) {
score.textContent =
score.textContent.slice(0, score.textContent.length - 3) +
e.target.textContent;
typedNumber = '';
lastCharacter = e.target.textContent;
}
//Change operator in equation
else if (
/[*\/+-]/.test(e.target.textContent) &&
/[*\/+-]/.test(lastCharacter)
) {
score.textContent =
score.textContent.slice(0, score.textContent.length - 1) +
e.target.textContent;
typedNumber = '';
lastCharacter = e.target.textContent;
}
//If a number ends with "." and user wants to use an operator the "." should be switched to the operator
else if (
/[*\/+-]/.test(e.target.textContent) &&
/[.]/.test(score.textContent[score.textContent.length - 1])
) {
score.textContent =
score.textContent.slice(0, score.textContent.length - 1) +
e.target.textContent;
typedNumber = '';
lastCharacter = e.target.textContent;
//Adding a digit to the equation. F prevents from getting functional keys (F1,F2 etc.)
} else if (/[0-9]/.test(e.target.textContent)) {
score.textContent += e.target.textContent;
typedNumber += e.target.textContent;
lastCharacter = e.target.textContent;
//Adding decimal operator to the number
} else if (/[.]/.test(e.target.textContent) && !typedNumber.includes('.')) {
//Add "." if there are already digits in the number
if (typedNumber !== '') {
score.textContent += '.';
typedNumber += '.';
//Create 0 and then add "." if there is no number yet
} else {
score.textContent += '0.';
typedNumber += '0.';
}
lastCharacter = ',';
//Add an operator to the equation
} else if (
/[*\/+-]/.test(e.target.textContent) &&
(typedNumber || score.textContent)
) {
score.textContent += e.target.textContent;
typedNumber = '';
lastCharacter = e.target.textContent;
}
}
//Cancelling
let cancel = document.querySelector('#C');
document.addEventListener('keydown', (e) => {
if (['c', 'C', 'Escape', 'Backspace', 'Delete'].includes(e.key)) {
cancelEquation();
}
});
cancel.addEventListener('click', cancelEquation);
function cancelEquation() {
score.textContent = '';
typedNumber = '';
lastCharacter = '';
}
//Solving equation
let equation = document.querySelector('#equation');
document.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
solveEquation();
}
});
equation.addEventListener('click', solveEquation);
function solveEquation() {
if (score.textContent === '0.') {
score.textContent = '';
typedNumber = '';
lastCharacter = '';
}
score.textContent = eval(score.textContent);
typedNumber = score.textContent;
}