-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.js
146 lines (135 loc) · 4.59 KB
/
calc.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
const numberButtons = document.querySelectorAll('[data-number]');
const mathOperatorButtons = document.querySelectorAll('[data-operation]');
const allClearButton = document.querySelector('[data-all-clear]');
const deleteButton = document.querySelector('[data-delete]');
const equalsButton = document.querySelector('[data-equals]');
const prevOutput = document.querySelector('[data-prev-operand]');
const curOutput = document.querySelector('[data-cur-operand]');
prevOutput.innerText = '';
curOutput.innerText = '';
let newPrevOutput = '';
let newCurOutput = '0';
let mathOperator = '';
function updateDisplay() {
prevOutput.innerText = newPrevOutput;
curOutput.innerText = newCurOutput;
}
updateDisplay();
function calculate() {
let result;
newPrevOutput = parseFloat(newPrevOutput);
newCurOutput = parseFloat(newCurOutput);
if (Number.isNaN(newPrevOutput) || Number.isNaN(newCurOutput)) return;
if (mathOperator === '/') {
if (newCurOutput === 0) {
result = 'Math Err!';
} else {
result = newPrevOutput / newCurOutput;
}
} else if (mathOperator === '*' || mathOperator === 'x') {
result = newPrevOutput * newCurOutput;
} else if (mathOperator === '+') {
result = newPrevOutput + newCurOutput;
} else if (mathOperator === '-') {
result = newPrevOutput - newCurOutput;
} else {
return;
}
newCurOutput = result;
mathOperator = undefined;
newPrevOutput = '';
}
function appendNumber(number) {
if (newCurOutput === '0' && number === '0') return;
if (newCurOutput.includes('.') && number === '.') return;
if (newCurOutput === '0' && number >= '1' && number <= '9') {
newCurOutput = number.toString();
} else if ((newCurOutput === '' || newCurOutput === 'Math Err!') && number === '.') {
newCurOutput = `0${number.toString()}`;
} else if (newCurOutput === 'Math Err!' && number !== '') {
newCurOutput = number.toString();
} else if (newCurOutput !== '' && number) {
newCurOutput += number.toString();
} else {
newCurOutput = newCurOutput.toString() + number.toString();
}
}
function chooseOperator(operator) {
if ((newCurOutput === 'Math Err!' || newCurOutput === '') && operator) return;
if (newPrevOutput.includes('Math Err!')) {
newCurOutput = newPrevOutput.slice(0, -3);
newPrevOutput = '';
return;
}
if (newCurOutput === '0' && operator) {
newPrevOutput = `${newCurOutput} ${operator} `;
}
if (newPrevOutput !== '') calculate();
mathOperator = operator;
newPrevOutput = `${newCurOutput} ${operator} `;
newCurOutput = '0';
}
function clearAll() {
while (prevOutput.innerText === '' && curOutput.innerText === '0') return;
prevOutput.innerText = '';
curOutput.innerText = '0';
newPrevOutput = '';
newCurOutput = '0';
mathOperator = '';
}
function deleteNum() {
if (newCurOutput === 'Math Err!') return;
if ((newCurOutput === '0' && newPrevOutput === '') || (newPrevOutput === '' && newCurOutput === '')) return;
if (newPrevOutput !== '' && (newCurOutput === '' || newCurOutput === '0')) {
newPrevOutput = newPrevOutput.slice(0, -3);
newCurOutput = newPrevOutput;
newPrevOutput = '';
} else {
newCurOutput = newCurOutput.toString().slice(0, -1);
}
updateDisplay();
}
function keyboardInput(e) {
const operators = ['/', '*', 'x', '-', '+'];
if (operators.includes(e.key)) chooseOperator(e.key);
if ((e.key >= 0 && e.key <= 9) || e.key === '.') appendNumber(e.key);
if (e.key === 'Escape') clearAll();
if (e.key === 'Backspace') deleteNum();
if (e.key === 'Enter' || e.key === '=') {
if ((newCurOutput === '' && newPrevOutput === '') || (newCurOutput !== '' && newPrevOutput === '')) return;
if (newPrevOutput.includes('Math Err!')) {
newCurOutput = newPrevOutput.slice(0, -3);
newPrevOutput = '';
updateDisplay();
return;
}
calculate();
}
updateDisplay();
}
numberButtons.forEach((button) => {
button.addEventListener('click', () => {
appendNumber(button.innerText);
updateDisplay();
});
});
mathOperatorButtons.forEach((button) => {
button.addEventListener('click', () => {
chooseOperator(button.innerText);
updateDisplay();
});
});
equalsButton.addEventListener('click', () => {
if (newPrevOutput.includes('Math Err!')) {
newCurOutput = newPrevOutput.slice(0, -3);
newPrevOutput = '';
updateDisplay();
return;
}
if ((newCurOutput === '' && newPrevOutput === '') || (newCurOutput !== '' && newPrevOutput === '')) return;
calculate();
updateDisplay();
});
allClearButton.addEventListener('click', clearAll);
deleteButton.addEventListener('click', deleteNum);
document.addEventListener('keydown', keyboardInput);