-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
156 lines (144 loc) · 4.34 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
// A web application for a calculator
const MAX_LENGTH = 11;
let firstOperand = "";
let secondOperand = "";
let currentOperator = null;
let resetDisplay = false;
const display = document.querySelector(".display");
const numberButtons = document.querySelectorAll(".number");
const operatorButtons = document.querySelectorAll(".operator");
const equalButton = document.querySelector(".equal");
function applyOperator(operator, firstNumber, secondNumber) {
switch (operator) {
case "+":
return firstNumber + secondNumber;
case "-":
return firstNumber - secondNumber;
case "*":
return firstNumber * secondNumber;
case "/":
return firstNumber / secondNumber;
}
}
// Truncate the current operand if it is too long
function truncateOperand(operand) {
operand = operand.toString().slice(0, MAX_LENGTH);
if (operand[-1] === ".") {
operand = operand.slice(0, -1);
}
return operand;
}
// Add event listeners to number buttons
function appendOperand(operand, digit) {
if (
(digit === "0" && operand === "0") ||
(digit === "00" && (operand === "" || operand === "0"))
) {
return operand;
}
operand += digit;
operand = truncateOperand(operand);
display.textContent = operand;
return operand;
}
numberButtons.forEach((button) => {
button.addEventListener("click", () => {
if (currentOperator === null) {
if (resetDisplay) {
firstOperand = "";
resetDisplay = false;
}
firstOperand = appendOperand(firstOperand, button.value);
} else {
secondOperand = appendOperand(secondOperand, button.value);
}
});
});
// Evaluate the current expression if possible
function evaluate() {
if (currentOperator === null || secondOperand === "") {
firstOperand = Number(firstOperand);
} else {
firstOperand = applyOperator(
currentOperator,
Number(firstOperand),
Number(secondOperand)
);
}
firstOperand = truncateOperand(firstOperand);
currentOperator = null;
secondOperand = "";
display.textContent = firstOperand;
resetDisplay = true;
}
// Add event listeners to operator buttons
operatorButtons.forEach((button) => {
button.addEventListener("click", () => {
evaluate();
currentOperator = button.value;
});
});
// Add event listener to equal button
equalButton.addEventListener("click", evaluate);
// Add event listener to all clear button
const allClearButton = document.querySelector(".all-clear");
allClearButton.addEventListener("click", () => {
firstOperand = "";
secondOperand = "";
currentOperator = null;
display.textContent = "";
});
// Add event listener to sign button
const signButton = document.querySelector(".sign");
function changeOperandSign(operand) {
if (operand === "") {
return "";
}
operand = "-" + operand;
display.textContent = operand;
return operand;
}
signButton.addEventListener("click", () => {
if (currentOperator === null) {
firstOperand = changeOperandSign(firstOperand);
} else {
secondOperand = changeOperandSign(secondOperand);
}
});
// Add event listener to decimal button
const decimalButton = document.querySelector(".decimal");
function appendDecimal(operand) {
if (operand === "" || operand.includes(".")) {
return operand;
}
operand += ".";
operand = truncateOperand(operand);
display.textContent = operand;
return operand;
}
decimalButton.addEventListener("click", () => {
if (currentOperator === null) {
firstOperand = appendDecimal(firstOperand);
} else {
secondOperand = appendDecimal(secondOperand);
}
});
// Add event listener to percent button
const percentButton = document.querySelector(".percent");
function convertToPercent(operand) {
if (operand === "") {
return operand;
}
operand = Number(operand) / 100;
operand = truncateOperand(operand);
operand = Number(operand).toString();
display.textContent = operand;
return operand;
}
percentButton.addEventListener("click", () => {
if (currentOperator === null) {
firstOperand = convertToPercent(firstOperand);
} else {
secondOperand = convertToPercent(secondOperand);
}
});