Skip to content

Commit 2cc258b

Browse files
committed
add norm-calculator
1 parent 6cb75de commit 2cc258b

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

public/norm-calculator.html

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>직각삼각형 계산기</title>
7+
<style>
8+
body {
9+
font-family: sans-serif;
10+
padding: 20px;
11+
}
12+
main {
13+
max-width: 300px;
14+
display: flex;
15+
height: 100svh;
16+
flex-direction: column;
17+
align-items: center;
18+
}
19+
label {
20+
display: flex;
21+
margin-top: 10px;
22+
align-items: baseline;
23+
&>input {
24+
flex: 1;
25+
padding: 5px;
26+
margin-top: 5px;
27+
}
28+
&>span {
29+
display: block;
30+
margin-right: 5px;
31+
}
32+
}
33+
.result {
34+
margin-top: 15px;
35+
font-weight: bold;
36+
}
37+
.formula {
38+
font-family: monospace;
39+
margin-bottom: 10px;
40+
}
41+
</style>
42+
</head>
43+
<body>
44+
<main>
45+
<div class="formula">c² = a² + b²<br />c = √(a² + b²)</div>
46+
47+
<label><span>a = </span><input type="number" id="a" step="any" /></label>
48+
<label><span>b = </span><input type="number" id="b" step="any" /></label>
49+
<label><span>c = </span><input type="number" id="c" step="any" /></label>
50+
51+
<div class="result" id="result"></div>
52+
</main>
53+
54+
<script>
55+
const fields = ['a', 'b', 'c'];
56+
const values = { a: NaN, b: NaN, c: NaN };
57+
const history = { a: null, b: null, c: null };
58+
59+
fields.forEach(id => {
60+
document.getElementById(id).addEventListener('input', () => {
61+
const val = parseFloat(document.getElementById(id).value);
62+
values[id] = isNaN(val) ? NaN : val;
63+
history[id] = new Date();
64+
calculateFromRecent();
65+
});
66+
});
67+
68+
function calculateFromRecent() {
69+
const result = document.getElementById('result');
70+
71+
const sorted = fields
72+
.filter(f => history[f] instanceof Date)
73+
.sort((a, b) => history[b] - history[a]);
74+
75+
if (sorted.length < 2) {
76+
result.textContent = '';
77+
return;
78+
}
79+
80+
const [f1, f2] = sorted;
81+
const missing = fields.find(f => f !== f1 && f !== f2);
82+
const v1 = values[f1];
83+
const v2 = values[f2];
84+
85+
if (isNaN(v1) || isNaN(v2)) {
86+
result.textContent = '';
87+
return;
88+
}
89+
90+
let value;
91+
if (missing === 'c') {
92+
value = Math.sqrt(values['a'] ** 2 + values['b'] ** 2);
93+
document.getElementById('c').value = value.toFixed(4);
94+
result.textContent = `c = ${value.toFixed(4)}`;
95+
} else if (missing === 'a') {
96+
if (values['c'] <= values['b']) {
97+
result.textContent = 'c는 b보다 커야 합니다.';
98+
return;
99+
}
100+
value = Math.sqrt(values['c'] ** 2 - values['b'] ** 2);
101+
document.getElementById('a').value = value.toFixed(4);
102+
result.textContent = `a = ${value.toFixed(4)}`;
103+
} else if (missing === 'b') {
104+
if (values['c'] <= values['a']) {
105+
result.textContent = 'c는 a보다 커야 합니다.';
106+
return;
107+
}
108+
value = Math.sqrt(values['c'] ** 2 - values['a'] ** 2);
109+
document.getElementById('b').value = value.toFixed(4);
110+
result.textContent = `b = ${value.toFixed(4)}`;
111+
}
112+
}
113+
</script>
114+
</body>
115+
</html>

0 commit comments

Comments
 (0)