Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
93 lines (82 loc) · 2.66 KB
/
Copy pathscript.js
File metadata and controls
93 lines (82 loc) · 2.66 KB
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
const currentEl = document.getElementById('current');
const previousEl = document.getElementById('previous');
const keys = document.querySelector('.keys');
let current = '0';
let previous = '';
let operator = null;
let resetNext = false;
function updateDisplay() {
currentEl.textContent = current;
if (operator) {
const opMap = { '+': '+', '-': '−', '*': '×', '/': '÷' };
previousEl.textContent = `${previous} ${opMap[operator] || operator}`;
} else {
previousEl.textContent = '';
}
}
function inputNumber(num) {
if (resetNext) { current = ''; resetNext = false; }
if (num === '.' && current.includes('.')) return;
if (current === '0' && num !== '.') { current = num; }
else { current += num; }
updateDisplay();
}
function chooseOperator(op) {
if (current === '' && previous === '') return;
if (previous !== '' && operator && !resetNext) { compute(); }
operator = op;
previous = current;
resetNext = true;
updateDisplay();
}
function compute() {
if (previous === '' || operator === null) return;
const a = parseFloat(previous);
const b = parseFloat(current);
let result;
switch (operator) {
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/': result = b === 0 ? 'Error' : a / b; break;
default: return;
}
current = String(Math.round(result * 1e10) / 1e10);
operator = null;
previous = '';
resetNext = true;
updateDisplay();
}
function clearAll() {
current = '0'; previous = ''; operator = null; resetNext = false;
updateDisplay();
}
function deleteLast() {
if (resetNext) return;
if (current.length > 1) { current = current.slice(0, -1); }
else { current = '0'; }
updateDisplay();
}
function percent() {
current = String(parseFloat(current) / 100);
updateDisplay();
}
keys.addEventListener('click', (e) => {
const key = e.target.closest('.key');
if (!key) return;
if (key.dataset.number !== undefined) inputNumber(key.dataset.number);
else if (key.dataset.operator) chooseOperator(key.dataset.operator);
else if (key.dataset.action === 'equals') compute();
else if (key.dataset.action === 'clear') clearAll();
else if (key.dataset.action === 'delete') deleteLast();
else if (key.dataset.action === 'percent') percent();
});
document.addEventListener('keydown', (e) => {
if (e.key >= '0' && e.key <= '9') inputNumber(e.key);
else if (e.key === '.') inputNumber('.');
else if (['+', '-', '*', '/'].includes(e.key)) chooseOperator(e.key);
else if (e.key === 'Enter' || e.key === '=') { e.preventDefault(); compute(); }
else if (e.key === 'Backspace') deleteLast();
else if (e.key === 'Escape') clearAll();
});
updateDisplay();