-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
139 lines (120 loc) · 3.86 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
let firstOperand = "";
let secondOperand = "";
let currentOperation = null;
let shouldResetScreen = false;
const numberButtons = document.querySelectorAll("[data-number]");
const operatorButtons = document.querySelectorAll("[data-operator]");
const equalsButton = document.getElementById("equalsBtn");
const clearButton = document.getElementById("clearBtn");
const deleteButton = document.getElementById("deleteBtn");
const pointButton = document.getElementById("pointBtn");
const lastOperationScreen = document.getElementById("lastOperationScreen");
const currentOperationScreen = document.getElementById(
"currentOperationScreen"
);
window.addEventListener("keydown", handleKeyboardInput);
equalsButton.addEventListener("click", evaluate);
clearButton.addEventListener("click", clear);
deleteButton.addEventListener("click", deleteNumber);
pointButton.addEventListener("click", appendPoint);
numberButtons.forEach((button) =>
button.addEventListener("click", () => appendNumber(button.textContent))
);
operatorButtons.forEach((button) =>
button.addEventListener("click", () => setOperation(button.textContent))
);
function appendNumber(number) {
if (currentOperationScreen.textContent === "0" || shouldResetScreen)
resetScreen();
currentOperationScreen.textContent += number;
}
function resetScreen() {
currentOperationScreen.textContent = "";
shouldResetScreen = false;
}
function clear() {
currentOperationScreen.textContent = "0";
lastOperationScreen.textContent = "";
firstOperand = "";
secondOperand = "";
currentOperation = null;
}
function appendPoint() {
if (shouldResetScreen) resetScreen();
if (currentOperationScreen.textContent === "")
currentOperationScreen.textContent = "0";
if (currentOperationScreen.textContent.includes(".")) return;
currentOperationScreen.textContent += ".";
}
function deleteNumber() {
currentOperationScreen.textContent = currentOperationScreen.textContent
.toString()
.slice(0, -1);
}
function setOperation(operator) {
if (currentOperation !== null) evaluate();
firstOperand = currentOperationScreen.textContent;
currentOperation = operator;
lastOperationScreen.textContent = `${firstOperand} ${currentOperation}`;
shouldResetScreen = true;
}
function evaluate() {
if (currentOperation === null || shouldResetScreen) return;
if (currentOperation === "÷" && currentOperationScreen.textContent === "0") {
alert("You can't divide by 0!");
return;
}
secondOperand = currentOperationScreen.textContent;
currentOperationScreen.textContent = roundResult(
operate(currentOperation, firstOperand, secondOperand)
);
lastOperationScreen.textContent = `${firstOperand} ${currentOperation} ${secondOperand} =`;
currentOperation = null;
}
function roundResult(number) {
return Math.round(number * 1000) / 1000;
}
function handleKeyboardInput(e) {
if (e.key >= 0 && e.key <= 9) appendNumber(e.key);
if (e.key === ".") appendPoint();
if (e.key === "=" || e.key === "Enter") evaluate();
if (e.key === "Backspace") deleteNumber();
if (e.key === "Escape") clear();
if (e.key === "+" || e.key === "-" || e.key === "*" || e.key === "/")
setOperation(convertOperator(e.key));
}
function convertOperator(keyboardOperator) {
if (keyboardOperator === "/") return "÷";
if (keyboardOperator === "*") return "×";
if (keyboardOperator === "-") return "−";
if (keyboardOperator === "+") return "+";
}
function add(a, b) {
return a + b;
}
function substract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
function divide(a, b) {
return a / b;
}
function operate(operator, a, b) {
a = Number(a);
b = Number(b);
switch (operator) {
case "+":
return add(a, b);
case "−":
return substract(a, b);
case "×":
return multiply(a, b);
case "÷":
if (b === 0) return null;
else return divide(a, b);
default:
return null;
}
}