-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
142 lines (126 loc) · 4.06 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
let displayValue = '';
let firstOperand = null;
let secondOperand = null;
let currentOperation = null;
let shouldResetDisplay = false;
alert(
"Hello! I am a simple calculator.\nI can easily calculate any numbers up to 8 digits.\n" +
"Here is how to use me:\n\n" +
"1. To enter numbers, simply click the buttons or use the number keys on your keyboard (0-9).\n" +
"2. To input decimal points, click the '.' button or use the period key on your keyboard.\n" +
"3. To perform calculations, click one of the operation buttons (+, -, *, /), or use the corresponding keyboard keys.\n" +
"4. To calculate the result, press '=' or hit 'Enter' on your keyboard.\n" +
"5. Use 'Backspace' on your keyboard or the 'C' button to delete the last digit.\n" +
"6. Press 'Esc' to clear the entire display.\n" +
"7. To calculate percentages, click the '%' button or use the '%' key on your keyboard. This divides the current number by 100.\n\n" +
"Enjoy calculating! 😊"
);
const displayElement = document.getElementById('display');
displayElement.value = displayValue || '0';
const buttons = document.querySelectorAll('.grid-container button');
function triggerButtonAnimation(key) {
const button = [...buttons].find(button => button.textContent === key);
if (button) {
button.classList.add('active');
setTimeout(() => {
button.classList.remove('active');
}, 100);
}
}
document.addEventListener('keydown', (event) => {
const key = event.key;
if (key >= '0' && key <= '9') {
appendNumber(key);
triggerButtonAnimation(key);
} else if (key === '.') {
appendDecimal();
triggerButtonAnimation(key);
} else if (key === 'Enter') {
calculate();
triggerButtonAnimation('=');
} else if (key === 'Backspace') {
clearNumber();
triggerButtonAnimation('Backspace');
} else if (key === 'Escape') {
clearDisplay();
triggerButtonAnimation('Escape');
} else if (key === '+' || key === '-' || key === '*' || key === '/') {
setOperation(key);
triggerButtonAnimation(key);
} else if (key === '%') {
percent();
triggerButtonAnimation('%');
}
});
function clearDisplay() {
displayValue = '';
firstOperand = null;
secondOperand = null;
currentOperation = null;
shouldResetDisplay = false;
updateDisplay();
}
function clearNumber() {
displayValue = displayValue.slice(0, -1);
updateDisplay();
}
function appendNumber(number) {
if (shouldResetDisplay) {
displayValue = '';
shouldResetDisplay = false;
}
if (displayValue.length < 9) {
displayValue += number;
updateDisplay();
}
}
function appendDecimal() {
if (!displayValue.includes('.')) {
displayValue += '.';
updateDisplay();
}
}
function setOperation(operation) {
if (currentOperation !== null) calculate();
firstOperand = parseFloat(displayValue);
currentOperation = operation;
shouldResetDisplay = true;
}
function calculate() {
if (currentOperation === null || shouldResetDisplay) return;
secondOperand = parseFloat(displayValue);
let result;
switch (currentOperation) {
case '+':
result = firstOperand + secondOperand;
break;
case '-':
result = firstOperand - secondOperand;
break;
case '*':
result = firstOperand * secondOperand;
break;
case '/':
if (secondOperand === 0) {
result = 'Error';
} else {
result = firstOperand / secondOperand;
}
break;
default:
return;
}
displayValue = String(result).slice(0, 9);
currentOperation = null;
updateDisplay();
}
function percent() {
if (firstOperand !== null && currentOperation === null) {
displayValue = String(parseFloat(displayValue) / 100);
updateDisplay();
}
}
function updateDisplay() {
displayElement.value = displayValue || '0';
}
clearDisplay();