-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrpit.js
49 lines (42 loc) · 1.11 KB
/
scrpit.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
let displayValue = '';
function appendNumber(number) {
displayValue += number;
updateDisplay();
}
function appendOperator(operator) {
if (displayValue === '') return;
const lastChar = displayValue[displayValue.length - 1];
if ('+-*/%'.includes(lastChar)) {
displayValue = displayValue.slice(0, -1);
}
displayValue += operator;
updateDisplay();
}
function appendDot() {
if (displayValue === '' || /[+\-*/%]/.test(displayValue[displayValue.length - 1])) {
displayValue += '0.';
} else if (!displayValue.includes('.')) {
displayValue += '.';
}
updateDisplay();
}
function clearDisplay() {
displayValue = '';
updateDisplay();
}
function deleteChar() {
displayValue = displayValue.slice(0, -1);
updateDisplay();
}
function calculate() {
try {
displayValue = eval(displayValue).toString();
} catch {
displayValue = 'Error';
}
updateDisplay();
}
function updateDisplay() {
const display = document.getElementById('display');
display.value = displayValue;
}