-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.js
179 lines (141 loc) · 3.49 KB
/
calculator.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
const calculator = document.querySelector('.calc-bg');
const calculatorOutput = calculator.querySelector('output');
const operators = ['+', '-', 'x', '/'];
let userInput,
lastResult,
operator;
function isFloat(num) {
if (num !== Infinity) {
return (num).toString().includes('.');
}
return false;
}
// Arithmetic Functions
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;
const multiply = (a, b) => a * b;
function divide(a, b) {
if (parseFloat(a) !== 0 || !isNaN(a) ||
parseFloat(b) !== 0 || !isNaN(b)) {
return a / b;
}
}
function operate(operator, a, b) {
switch (operator) {
case "+":
return add(a, b);
case "-":
return subtract(a, b);
case "x":
return multiply(a, b);
case "/":
return divide(a, b);
}
}
// Helper and handler functions
function updateOutput(text) {
calculatorOutput.innerText = text;
}
function updateOperator(newOperator) {
operator = newOperator;
}
function clearCalculator() {
userInput = '';
lastResult = '';
operator = '';
updateOutput('0');
}
function backSpace() {
userInput = userInput.slice(0, userInput.length - 1);
if (!userInput) {
updateOutput('0');
return;
}
updateOutput(userInput);
}
function numberInput(num) {
if (!userInput || userInput === '0') {
userInput = num;
} else {
userInput += num;
}
updateOutput(userInput);
}
function periodInput() {
if (!userInput) {
userInput = '0.';
} else if (userInput.match(/^\d+$/)) {
userInput += '.';
}
updateOutput(userInput);
}
function changeInputSign() {
if (!isNaN(userInput)) {
userInput = (userInput * -1).toString();
updateOutput(userInput);
}
}
// MAIN OPERATING FUNCTIONALITY
function startNewOperation(newOperator) {
updateOperator(newOperator);
lastResult = Number(userInput);
updateOutput(operator);
}
function operateWithPreviousResult(newOperator) {
// TODO: We should truncate the output of floats 'intelligently'
lastResult = operate(operator, lastResult, Number(userInput));
if (isFloat(lastResult)) {
lastResult = parseFloat(lastResult.toFixed(4));
}
updateOutput(lastResult);
updateOperator(newOperator);
}
function operatorInput(newOperator) {
if (!operator && userInput) {
startNewOperation(newOperator);
} else if (lastResult && userInput) {
operateWithPreviousResult(newOperator);
} else {
updateOperator(newOperator);
updateOutput(operator);
}
userInput = undefined;
}
function equals() {
if (lastResult && userInput) {
operateWithPreviousResult('');
userInput = undefined;
}
}
function handleInput(input) {
if (!isNaN(input) && input !== ' ') {
numberInput(input);
}
if (input === '.') {
periodInput();
}
if (!input || input === 'Backspace') {
backSpace();
}
if (operators.includes(input)) {
operatorInput(input);
}
if (input === 'Sign') {
changeInputSign();
}
if (input === '=' || input === 'Enter') {
equals();
}
if (input.match(/^[A|C]/i)) {
clearCalculator();
}
}
// Event listeners
calculator.addEventListener('click', e => {
if (e.target.nodeName === 'BUTTON') {
handleInput(e.target.innerText);
}
});
document.addEventListener('keydown', (e) => {
handleInput(e.key);
});