-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
254 lines (229 loc) · 7.07 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//add number key buttons and event listener
const numberButtons = document.querySelectorAll('.number-button');
numberButtons.forEach(numberButton => numberButton.addEventListener('click', numberInput));
//add calculation input buttons and event listener
const operatorButtons = document.querySelectorAll('.operator-button');
operatorButtons.forEach(functionButton => functionButton.addEventListener('click', operatorInput));
//add calc and clear button
document.getElementById('calc').addEventListener('click', operate);
document.getElementById('clear').addEventListener('click', clear);
window.addEventListener('keydown', numpadInput);
//create arrays and variables
var numberArray = [];
var displayArray = [];
let num, firstNum, secondNum, number, firstOperator, secondOperator, answer;
let count = 0;
let numberCount = 0;
let calculationCount = 0;
let decimalCount = 0;
let decimalCountIndex;
let calc=false;
function numpadInput(e){
if(e.key=='+'||e.key=='-'||e.key=='*'||e.key=='/'||e.key=='End') {
if(e.key=='+') status='+';
if(e.key=='-') status='-';
if(e.key=='*') status='*';
if(e.key=='/') status='/';
if(e.key=='End') status='+/-';
operatorInput(e);
}
if(!isNaN(e.key)){
if(e.key=='0') num=0;
if(e.key=='1') num=1;
if(e.key=='2') num=2;
if(e.key=='3') num=3;
if(e.key=='4') num=4;
if(e.key=='5') num=5;
if(e.key=='6') num=6;
if(e.key=='7') num=7;
if(e.key=='8') num=8;
if(e.key=='9') num=9;
if(e.key=='0') num=0;
numberInput(e);
}
if(e.key=='.'){
num='.';
numberInput();
}
if(e.key=='Enter') operate(e);
if(e.key=='Delete') clear();
}
function numberInput(e) {
if(e) {
if(e.type=='click') num = e.target.innerText;
}
if (decimalCount == 0 || !(num == '.')) {
count++;
numberArray[count] = num;
displayArrays(num);
number = parseFloat(numberArray.join(''));
}
if (num == '.') {
decimalCountIndex = count;
decimalCount = 1;
}
if(calc && !isNaN(displayArray[numberCount-1])) {
refreshScreen();
solutionDisplay('');
secondNum='';
firstOperator='';
secondOperator='';
answer='';
calc=false;
document.getElementById('text').textContent ="New calculation here, time to refresh the screen!";
}
}
function operatorInput(e) {
calc=false;
if(e){
if(e.type=='click') status = e.target.id;
}
//call to set negative or positive number
if (status == '+/-'){
status='';
if(number) {
number = negativeNumber(number);
}
}
else
{
if ((!firstOperator && !isNaN(displayArray[numberCount-1]) || (firstOperator && !isNaN(displayArray[numberCount-1])))) {
displayArrays(status);
}
//register the latest operator input during multiple input
if (firstOperator && !number) {
numberCount--;
displayArrays(status);
calculationCount = 0;
firstOperator = '';
}
operate();
}
status = '';
}
//change number into positive or negative value
function negativeNumber(num) {
document.getElementById('text').textContent ="Changing positive/negative value of the number here...";
num = num * -1;
for (i = numberCount - count; i <= numberCount; i++) {
displayArray[i] = '';
}
displayArrays(num);
if(!(numberArray[0]=='-')){
numberArray.splice(0,0,'-');
count++;
}
else if (numberArray[0]=='-') {
numberArray.splice(0,1);
}
return num;
}
function operate(e) {
document.getElementById('text').textContent ="You can use numeric keyboard too. Believe me, it's more fun!";
//assign numbers and operator into variables into first and second order
if (!firstOperator && calculationCount === 0) {
if (number) firstNum = number;
firstOperator = status;
}
if (firstOperator && firstNum && calculationCount === 1) {
secondNum = number;
secondOperator = status;
}
calculationCount = 1;
if (e) {
calc=true;
calculationCount = 0;
}
//setting the only number input as answer
if (firstNum && !firstOperator && !secondNum & !secondOperator && e) {
answer = firstNum;
}
if (firstNum && firstOperator && e && isNaN(displayArray[numberCount-1])) {
answer='ERROR';
}
//call calculation functions according to operator input
if(firstNum && secondNum){
if (firstOperator === '+') answer = add(firstNum, secondNum);
if (firstOperator === '-') answer = subtract(firstNum, secondNum);
if (firstOperator === '*') answer = multiply(firstNum, secondNum);
if (firstOperator === '/') {
if ((secondNum === 0)) answer = 'ERROR';
if (!(secondNum === 0)) answer = divide(firstNum, secondNum);
}
}
//assign value shifting after calculation and call solution display
if (!isNaN(answer)) {
if (!(answer == Math.floor(answer)) && !(isNaN(answer)) && ((firstOperator == '*') || firstOperator == '/')) {
answer = answer.toFixed(8);
}
solutionDisplay(answer);
if(secondNum) firstNum = parseFloat(answer);
if (secondOperator) firstOperator = secondOperator;
secondOperator = '';
secondNum = '';
}
else if(answer=='ERROR') solutionDisplay(answer);
else if (e && firstNum) {
answer=firstNum;
solutionDisplay(answer);
}
numberArray = [];
count = 0;
number = '';
decimalCount = 0;
}
//display calculation on screen
function displayArrays(content) {
if(numberCount<27){
displayArray[numberCount] = `${content}`;
document.getElementById('display').textContent = displayArray.join("");
numberCount++;
}
if(numberCount==27) {
refreshScreen();
document.getElementById('text').textContent ="Opps the screen was too full. Refresh the screen!";
}
}
function refreshScreen(){
numberCount=0;
displayArray=[];
displayArray[numberCount]=num;
document.getElementById('display').textContent = displayArray.join("");
numberCount++;
}
//display solution on screen
function solutionDisplay(answer) {
document.getElementById('solution').textContent = `${answer}`;
}
//restore everything to original state
function clear() {
displayArray = [];
numberArray = [];
answer = '';
numberCount = 0;
calculationCount = 0;
count = 0;
decimalCount = 0;
firstNum = '';
secondNum = '';
firstOperator='';
secondOperator='';
num='';
number='';
decimalCountIndex='';
document.getElementById('display').textContent = '';
document.getElementById('solution').textContent = '';
}
//calculation functions
function add(firstNum, secondNum) {
return firstNum + secondNum;
}
function subtract(firstNum, secondNum) {
return firstNum - secondNum;
}
function multiply(firstNum, secondNum) {
return firstNum * secondNum;
}
function divide(firstNum, secondNum) {
return firstNum / secondNum;
}