1+ const celsiusInput = document . querySelector ( '#celsius > input' ) ;
2+ const fahrenheitInput = document . querySelector ( '#fahrenheit > input' ) ;
3+ const kelvinInput = document . querySelector ( '#kelvin > input' ) ;
4+
5+ const roundToTwoDP = ( num ) => {
6+ return num . toFixed ( 2 ) ;
7+ } ;
8+
9+
10+ const celsiusToFaAndKe = ( ) => {
11+ const celsiusTemp = parseFloat ( celsiusInput . value ) ;
12+ const fahrenheitTemp = ( celsiusTemp * 1.8 ) + 32 ;
13+ const kelvinTemp = celsiusTemp + 273.15 ;
14+
15+ fahrenheitInput . value = roundToTwoDP ( fahrenheitTemp ) ;
16+ kelvinInput . value = roundToTwoDP ( kelvinTemp ) ;
17+ } ;
18+
19+
20+ const fahrenheitToCeAndKe = ( ) => {
21+ const fahrenheitTemp = parseFloat ( fahrenheitInput . value ) ;
22+ const celsiusTemp = ( fahrenheitTemp - 32 ) * ( 5 / 9 ) ;
23+ const kelvinTemp = ( fahrenheitTemp + 459.67 ) * ( 5 / 9 ) ;
24+
25+ celsiusInput . value = roundToTwoDP ( celsiusTemp ) ;
26+ kelvinInput . value = roundToTwoDP ( kelvinTemp ) ;
27+ } ;
28+
29+ const kelvinToCeAndFa = ( ) => {
30+ const kelvinTemp = parseFloat ( kelvinInput . value ) ;
31+ const celsiusTemp = kelvinTemp - 273 ;
32+ const fahrenheitTemp = 1.8 * ( kelvinTemp - 273 ) + 32 ;
33+
34+ celsiusInput . value = roundToTwoDP ( celsiusTemp ) ;
35+ fahrenheitInput . value = roundToTwoDP ( fahrenheitTemp ) ;
36+ } ;
37+
38+
39+ const main = ( ) => {
40+ celsiusInput . addEventListener ( 'input' , celsiusToFaAndKe ) ;
41+ fahrenheitInput . addEventListener ( 'input' , fahrenheitToCeAndKe ) ;
42+ kelvinInput . addEventListener ( 'input' , kelvinToCeAndFa ) ;
43+ } ;
44+
45+ main ( ) ;
0 commit comments