Practice: Temperature Converter
A live page.
Now a practice project in the browser. Type a temperature in Celsius, and the page shows it in Fahrenheit and Kelvin straight away, with a short description of how it feels.

- A browser project
- Type Celsius
- See Fahrenheit and Kelvin
Converting as you type
Convert is a pure function. It takes Celsius, and returns an object with all three values. The input event calls update on every key press. If the text isn't a number, we show a message instead. Otherwise we display the results, and pick a description. We typed thirty, so the page shows eighty six Fahrenheit, and hot.
<link rel="stylesheet" href="style.css">
<h1>Temperature</h1>
<input id="c" placeholder="Celsius">
<p id="out" class="big">-</p>
<p id="feel"></p>
<script>
const convert = (c) => ({ c, f: c * 9 / 5 + 32, k: c + 273.15 });
const describe = (c) => c < 0 ? "freezing" : c < 15 ? "cold" : c < 25 ? "mild" : "hot";
document.querySelector("#c").addEventListener("input", (e) => {
const c = parseFloat(e.target.value);
if (Number.isNaN(c)) { out.textContent = "Type a number"; return; }
const t = convert(c);
out.textContent = `${t.f.toFixed(1)}°F · ${t.k.toFixed(2)}K`;
feel.textContent = `That feels ${describe(c)}.`;
});
</script>Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#f7df1e;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} small{color:crimson;display:block;font-size:16px} .big{font-size:64px;font-weight:800}Also uses actions.json
[{"type": "#c", "text": "30"}]Separate logic from display
Notice that convert and describe know nothing about the page. They just take numbers and return values. That makes them easy to test, and easy to reuse, maybe in a Node script, or a different page. The event handler is the only part that touches the DOM.
| key | value |
|---|---|
| convert() | pure logic |
| describe() | pure logic |
| Event handler | reads and writes the page |
| parseFloat | text to number |
Summary
The converter kept its maths in pure functions, and used one input listener to update the page. Next, a bank account class.
Key takeaways- Pure conversion
- input event
- Separate logic and display