-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
30 lines (25 loc) · 939 Bytes
/
script.js
File metadata and controls
30 lines (25 loc) · 939 Bytes
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
const celsius = document.getElementById("celsius");
const fahrenheit = document.getElementById("fahrenheit");
const kelvin = document.getElementById("kelvin");
function computeTemp(event) {
const currentValue = +event.target.value;
switch (event.target.name) {
case "celsius":
kelvin.value = (currentValue + 273.32).toFixed(2);
fahrenheit.value = (currentValue * 1.8 + 32).toFixed(2);
break;
case "fahrenheit":
celsius.value = ((currentValue - 32) / 1.8).toFixed(2);
kelvin.value = ((currentValue - 32) / 1.8 + 273.32).toFixed(2);
break;
case "kelvin":
celsius.value = (currentValue - 273.32).toFixed(2);
fahrenheit.value = ((currentValue - 273.32) * 1.8 + 32).toFixed(2);
break;
default:
break;
}
}
celsius.addEventListener("keydown", computeTemp);
fahrenheit.addEventListener("keydown", computeTemp);
kelvin.addEventListener("keydown", computeTemp);