Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
133 lines (120 loc) · 3.38 KB
/
Copy pathapp.js
File metadata and controls
133 lines (120 loc) · 3.38 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
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
/* Simple calculator logic (no eval for safety)
Buttons use data attributes:
- data-num for numbers and dot
- data-op for operators: + - * /
- data-fn for functions: clear, toggle-sign, percent, equals
*/
(() => {
const displayEl = document.getElementById('display');
const buttons = document.getElementById('buttons');
let current = '0'; // current displayed number (string)
let previous = null; // previous number (string)
let operator = null; // '+', '-', '*', '/'
let justEvaluated = false;
function render() {
displayEl.textContent = current;
}
function clearAll() {
current = '0';
previous = null;
operator = null;
justEvaluated = false;
render();
}
function appendNumber(n) {
if (justEvaluated) { current = '0'; justEvaluated = false; }
if (n === '.' && current.includes('.')) return;
if (current === '0' && n !== '.') current = n;
else current = current + n;
render();
}
function toggleSign() {
if (current === '0') return;
current = (current.startsWith('-') ? current.slice(1) : '-' + current);
render();
}
function percent() {
const v = parseFloat(current || '0');
current = String(v / 100);
render();
}
function chooseOp(op) {
if (operator && !justEvaluated) {
// chain calculation
compute();
}
previous = current;
current = '0';
operator = op;
justEvaluated = false;
}
function compute() {
if (!operator || previous === null) return;
const a = parseFloat(previous);
const b = parseFloat(current);
let res = 0;
if (isNaN(a) || isNaN(b)) return;
switch(operator) {
case '+': res = a + b; break;
case '-': res = a - b; break;
case '*': res = a * b; break;
case '/': res = b === 0 ? 'Error' : a / b; break;
}
current = (res === 'Error') ? 'Error' : String(roundIfNeeded(res));
previous = null;
operator = null;
justEvaluated = true;
render();
}
function roundIfNeeded(num) {
// keep up to 10 decimal places if needed
if (Number.isInteger(num)) return num;
return Math.round(num * 1e10) / 1e10;
}
// event delegation for buttons
buttons.addEventListener('click', (e) => {
const btn = e.target.closest('button');
if (!btn) return;
// functions
if (btn.dataset.fn) {
const fn = btn.dataset.fn;
if (fn === 'clear') clearAll();
else if (fn === 'toggle-sign') toggleSign();
else if (fn === 'percent') percent();
else if (fn === 'equals') compute();
return;
}
// number
if (btn.dataset.num !== undefined) {
appendNumber(btn.dataset.num);
return;
}
// operator
if (btn.dataset.op) {
chooseOp(btn.dataset.op);
return;
}
});
// keyboard support
window.addEventListener('keydown', (e) => {
if ((e.key >= '0' && e.key <= '9') || e.key === '.') {
appendNumber(e.key);
return;
}
if (e.key === 'Enter' || e.key === '=') { compute(); return; }
if (e.key === 'Backspace') {
// backspace removes last digit
if (justEvaluated) { clearAll(); return; }
current = current.length > 1 ? current.slice(0, -1) : '0';
render();
return;
}
if (['+', '-', '*', '/'].includes(e.key)) {
chooseOp(e.key);
return;
}
if (e.key === 'c' || e.key === 'C') { clearAll(); return; }
});
// initialize
clearAll();
})();