-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
175 lines (159 loc) · 4.78 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
let aValue = '';
let bValue = '';
let currentOperator = '';
let resetDisplay = true;
const numbers = document.querySelectorAll('.num');
const operators = document.querySelectorAll('.ops');
const decimalButton = document.getElementById('dec');
const deleteButton = document.getElementById('delete');
const clearButton = document.getElementById('clear');
const equalsButton = document.getElementById('equals');
const mainDisplay = document.getElementById('display');
const tapeDisplay = document.getElementById('tape');
numbers.forEach((num) => num.addEventListener('click', () => toDisplay(num.textContent)));
operators.forEach((operator) => operator.addEventListener('click', () => setOperator(operator.textContent)));
decimalButton.addEventListener('click', () => addDecimal());
deleteButton.addEventListener('click', () => backSpace());
clearButton.addEventListener('click', () => clearAll());
equalsButton.addEventListener('click', () => evaluate('Enter'));
window.addEventListener('keydown', (e) => getKeyValue(e.key));
function getKeyValue(key) {
if (key === '/' || key === '*' || key === '-' || key === '+') {
setOperator(key);
} else if (key === '.') {
addDecimal();
} else if (key >= 0 || key <= 9) {
toDisplay(key);
} else if (key === 'Enter') {
evaluate(key);
} else if (key === 'Delete' || key === 'Clear') {
clearAll();
} else if (key === 'Backspace') {
backSpace();
}
}
function addDecimal() {
if (!resetDisplay && mainDisplay.textContent.replace('.', '') === mainDisplay.textContent) {
mainDisplay.textContent += '.';
} else if (resetDisplay || mainDisplay.textContent === '0') {
mainDisplay.textContent = '0.'
resetDisplay = false;
}
}
function toDisplay(e) {
if (resetDisplay || mainDisplay.textContent === '0') {
mainDisplay.textContent = e;
resetDisplay = false;
} else {
mainDisplay.textContent += e;
}
}
function backSpace() {
if (!resetDisplay || mainDisplay.textContent === '0') {
mainDisplay.textContent = mainDisplay.textContent.slice(0, -1);
if (mainDisplay.textContent.length < 1) {
mainDisplay.textContent = 0;
}
}
}
function setOperator(op) {
if (currentOperator === '') {
aValue = mainDisplay.textContent;
currentOperator = op;
toTape();
resetDisplay = true;
} else {
if (resetDisplay) {
currentOperator = op;
toTape();
} else {
evaluate('setOp');
currentOperator = op;
return;
}
}
}
function toTape(input) {
let msg;
if (bValue === '') {
msg = `${aValue} ${currentOperator}`;
tapeDisplay.textContent = msg;
} else {
msg = `${aValue.toString()} ${currentOperator.toString()} ${bValue.toString()} = ${input}`;
tapeDisplay.textContent = msg;
}
}
function clearAll() {
aValue = '';
bValue = '';
currentOperator = '';
resetDisplay = true;
mainDisplay.textContent = '0';
tapeDisplay.textContent = '';
}
function evaluate(from) {
console.log(from);
if (from = 'Enter') {
if (currentOperator === '') {
return;
} else {
bValue = mainDisplay.textContent;
operate(currentOperator, aValue, bValue);
currentOperator = '';
bValue = '';
aValue = mainDisplay.textContent;
resetDisplay = true;
}
} else if (from = 'setOp') {
bValue = mainDisplay.textContent;
operate(currentOperator, aValue, bValue);
bValue = '';
aValue = mainDisplay.textContent;
}
}
// calculation functions - DO NOT EDIT --------------
function add(a, b) {
return parseFloat(a) + parseFloat(b);
}
function subtract(a, b) {
return parseFloat(a) - parseFloat(b);
}
function multiply(a, b) {
return parseFloat(a) * parseFloat(b);
}
function divide(a, b) {
return parseFloat(a) / parseFloat(b);
}
function operate(operator, a, b) {
let result;
switch(operator) {
case '+':
result = add(a, b);
break;
case '-':
result = subtract(a, b);
break;
case '*':
result = multiply(a, b);
break;
case '×':
result = multiply(a, b);
break;
case '/':
if (a === '0'){
alert("You can't divide by 0!");
return;
}
result = divide(a, b);
break;
case '÷':
if (a === '0'){
alert("You can't divide by 0!");
return;
}
result = divide(a, b);
}
result = Math.round(result * 10000000000) / 10000000000;
toTape(result);
mainDisplay.textContent = result;
}