-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
171 lines (158 loc) · 4.9 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
let firstNumber = null;
let secondNumber = null;
let currentNumberInput = "";
let operator = null;
let result = null;
let calculation = null;
let numberDisplay = document.querySelector(".result");
let calculationDisplay = document.querySelector(".calculation");
registerListeners();
function registerListeners() {
let buttonRows = document.querySelectorAll(".button-row");
document.addEventListener("keydown", onKeyDown)
for (const buttonRow of buttonRows) {
for (const button of buttonRow.children) {
if(button.classList.contains("invert")) {
button.addEventListener("click", invert);
} else if (button.classList.contains("equals")) {
button.addEventListener("click", operate)
} else if (button.classList.contains("number")) {
button.addEventListener("click", () => onNumberEntered(button.textContent));
} else if (button.classList.contains("operator")) {
button.addEventListener("click", () => onOperatorEntered(button.textContent));
} else if (button.classList.contains("clear")) {
button.addEventListener("click", clear);
} else if (button.classList.contains("decimal")) {
button.addEventListener("click", onDecimalPointEntered);
}
}
}
}
function onKeyDown(event) {
let numbers = "0123456789";
let operators = "+-*/:%×÷";
if (numbers.includes(event.key)) {
onNumberEntered(event.key);
} else if (operators.includes(event.key)) {
onOperatorEntered(event.key);
} else if (event.key.toLowerCase() === "enter") {
operate();
} else if (event.key.toLowerCase() === "c" || event.key.toLowerCase() === "delete") {
clear();
} else if (event.key === "." || event.key === ",") {
onDecimalPointEntered();
}
}
function operate() {
console.log(`Calculating: ${firstNumber} ${operator} ${secondNumber}`);
switch (operator) {
case "+":
add();
break;
case "-":
subtract();
break;
case "×":
case "*":
multiply();
break;
case "÷":
case ":":
case "/":
divide();
break;
case "%":
modulo();
break;
default:
return;
}
console.log("Rounding " + result);
result = Math.round(result * 10000) / 10000;
updateDisplay();
firstNumber = result;
currentNumberInput = "";
operator = null;
secondNumber = null;
result = null;
}
function onNumberEntered(numberString) {
let number = parseInt(numberString);
if (isNaN(number)) {
console.log("Number conversion failed: " + numberString);
return;
}
addInput(number.toString());
}
function onDecimalPointEntered() {
if (currentNumberInput.includes(".")) return;
addInput(".");
}
function addInput(input) {
currentNumberInput += input;
if (operator === null) {
firstNumber = parseFloat(currentNumberInput);
} else {
secondNumber = parseFloat(currentNumberInput);
}
updateDisplay();
}
function onOperatorEntered(operatorString) {
if (operatorString === null) return;
if (operatorString.length !== 1) return;
let validOperators = "+-×*/÷%";
if (validOperators.indexOf(operatorString) === -1) return;
console.log("Operator entered " + operatorString);
if (secondNumber !== null) {
operate();
}
operator = operatorString;
currentNumberInput = "";
updateDisplay();
}
function updateDisplay() {
calculation = `${firstNumber ?? ""} ${operator ?? ""} ${secondNumber ?? ""}`;
if (calculation.trim().length === 0) {
calculation = "0"
}
numberDisplay.textContent = result ?? secondNumber ?? firstNumber ?? 0;
calculationDisplay.textContent = calculation;
}
function clear() {
currentNumberInput = "";
firstNumber = null;
secondNumber = null;
calculation = null;
operator = null;
result = null;
updateDisplay();
}
function add() {
result = firstNumber + secondNumber;
}
function subtract() {
result = firstNumber - secondNumber;
}
function multiply() {
result = firstNumber * firstNumber;
}
function divide() {
if (secondNumber === 0) {
result = "Hell nah";
return;
}
result = firstNumber / secondNumber;
}
function modulo() {
result = firstNumber % secondNumber;
}
function invert() {
if (secondNumber !== null) {
secondNumber = -secondNumber;
currentNumberInput = secondNumber.toString();
} else if (firstNumber !== null) {
firstNumber = -firstNumber;
currentNumberInput = firstNumber.toString();
}
updateDisplay();
}