-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
65 lines (60 loc) · 2.13 KB
/
app.js
File metadata and controls
65 lines (60 loc) · 2.13 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
// Simple client-side calculator logic
function compute(op, a, b) {
switch (op) {
case 'add':
return a + b;
case 'sub':
return a - b;
case 'mul':
return a * b;
case 'div':
if (b === 0) throw new Error('division by zero');
return a / b;
default:
throw new Error('unsupported operation: ' + op);
}
}
function formatResult(r) {
if (Number.isFinite(r) && Math.abs(r - Math.round(r)) < 1e-9) return String(Math.round(r));
return String(r);
}
window.addEventListener('DOMContentLoaded', () => {
// Theme handling: restore or default to system preference
const themeToggle = document.getElementById('theme-toggle');
const root = document.documentElement;
const saved = localStorage.getItem('calc-theme');
const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
const current = saved || (prefersDark ? 'dark' : 'light');
if (current === 'dark') root.setAttribute('data-theme', 'dark');
themeToggle.addEventListener('click', () => {
const now = root.getAttribute('data-theme') === 'dark' ? 'light' : 'dark';
if (now === 'dark') root.setAttribute('data-theme', 'dark'); else root.removeAttribute('data-theme');
localStorage.setItem('calc-theme', now);
});
const form = document.getElementById('calc-form');
const op = document.getElementById('op');
const aEl = document.getElementById('a');
const bEl = document.getElementById('b');
const calcBtn = document.getElementById('calc');
const clearBtn = document.getElementById('clear');
const resultEl = document.getElementById('result');
calcBtn.addEventListener('click', () => {
let a = parseFloat(aEl.value);
let b = parseFloat(bEl.value);
if (Number.isNaN(a) || Number.isNaN(b)) {
resultEl.textContent = 'Error: enter two numbers';
return;
}
try {
const r = compute(op.value, a, b);
resultEl.textContent = formatResult(r);
} catch (e) {
resultEl.textContent = 'Error: ' + e.message;
}
});
clearBtn.addEventListener('click', () => {
aEl.value = '';
bEl.value = '';
resultEl.textContent = '—';
});
});