-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (34 loc) · 1.1 KB
/
index.js
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
const inputs = document.querySelectorAll("input");
const celsiusEl = document.getElementById("celsius");
const fehrenheitEl = document.getElementById("fahrenheit");
const kelvinEl = document.getElementById("kelvin");
inputs.forEach((input) => {
input.addEventListener("change", (e) => {
if (e.target.value !== "") {
const userInput = +e.target.value;
getConverteduserInput(e.target, userInput);
} else {
[celsiusEl, fehrenheitEl, kelvinEl].map((e) => {
e.value = "";
});
}
});
});
function getConverteduserInput(target, userInput) {
switch (target.id) {
case "celsius":
fehrenheitEl.value = (userInput * 1.8 + 32).toFixed(2);
kelvinEl.value = (userInput + 273.15).toFixed(2);
break;
case "fahrenheit":
celsiusEl.value = ((userInput - 32) * (5 / 9)).toFixed(2);
kelvinEl.value = ((userInput - 32) * (5 / 9) + 273.15).toFixed(2);
break;
case "kelvin":
celsiusEl.value = (userInput - 273.15).toFixed(2);
fehrenheitEl.value = ((userInput - 273.15) * 1.8 + 32).toFixed(2);
break;
default:
break;
}
}