-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
45 lines (33 loc) · 1.34 KB
/
main.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
37
38
39
40
41
42
43
44
45
const celsiusInput = document.querySelector('#celsius > input');
const fahrenheitInput = document.querySelector('#fahrenheit > input');
const kelvinInput = document.querySelector('#kelvin > input');
const roundToTwoDP = (num) => {
return num.toFixed(2);
};
const celsiusToFaAndKe = () => {
const celsiusTemp = parseFloat(celsiusInput.value);
const fahrenheitTemp = (celsiusTemp * 1.8) + 32;
const kelvinTemp = celsiusTemp + 273.15;
fahrenheitInput.value = roundToTwoDP(fahrenheitTemp);
kelvinInput.value = roundToTwoDP(kelvinTemp);
};
const fahrenheitToCeAndKe = () => {
const fahrenheitTemp = parseFloat(fahrenheitInput.value);
const celsiusTemp = (fahrenheitTemp - 32) * (5 / 9);
const kelvinTemp = (fahrenheitTemp + 459.67) * (5 / 9);
celsiusInput.value = roundToTwoDP(celsiusTemp);
kelvinInput.value = roundToTwoDP(kelvinTemp);
};
const kelvinToCeAndFa = () => {
const kelvinTemp = parseFloat(kelvinInput.value);
const celsiusTemp = kelvinTemp - 273;
const fahrenheitTemp = 1.8 * (kelvinTemp - 273) + 32;
celsiusInput.value = roundToTwoDP(celsiusTemp);
fahrenheitInput.value = roundToTwoDP(fahrenheitTemp);
};
const main = () => {
celsiusInput.addEventListener('input', celsiusToFaAndKe);
fahrenheitInput.addEventListener('input', fahrenheitToCeAndKe);
kelvinInput.addEventListener('input', kelvinToCeAndFa);
};
main();