1
+ document . getElementById ( "myForm" ) . addEventListener ( "submit" , function ( e ) {
2
+ e . preventDefault ( ) ;
3
+
4
+ let temperature = parseFloat ( document . getElementById ( "temperature" ) . value ) ;
5
+ let from = document . getElementById ( "list-from" ) . value ;
6
+ let to = document . getElementById ( "list-to" ) . value ;
7
+ let result = "" ;
8
+
9
+ console . log ( "temperature: " + temperature ) ;
10
+ console . log ( "from: " + from + " || to: " + to ) ;
11
+
12
+ if ( from === "C" && to === "C" ) {
13
+ result = temperature ;
14
+ } else if ( from === "C" && to === "F" ) {
15
+ result = ( temperature * 1.8 ) + 32 ;
16
+ } else if ( from === "C" && to === "K" ) {
17
+ result = temperature + 273.15 ;
18
+ } else if ( from === "F" && to === "C" ) {
19
+ result = ( temperature - 32 ) / 1.8 ;
20
+ } else if ( from === "F" && to === "F" ) {
21
+ result = temperature ;
22
+ } else if ( from === "F" && to === "K" ) {
23
+ result = ( temperature + 459.67 ) * ( 5 / 9 ) ;
24
+ } else if ( from === "K" && to === "C" ) {
25
+ result = temperature - 273.15 ;
26
+ } else if ( from === "K" && to === "F" ) {
27
+ result = ( temperature * ( 5 / 9 ) ) - 459.67 ;
28
+ } else if ( from === "K" && to === "K" ) {
29
+ result = temperature ;
30
+ }
31
+
32
+ if ( result === "" ) {
33
+ document . getElementById ( "result-from" ) . innerHTML = temperature + "°" + from + "=" ;
34
+ document . getElementById ( "result-to" ) . innerHTML = temperature + "°" + to ;
35
+ } else {
36
+ document . getElementById ( "result-from" ) . innerHTML = temperature + "°" + from + "=" ;
37
+ document . getElementById ( "result-to" ) . innerHTML = result . toFixed ( 2 ) + "°" + to ;
38
+ }
39
+ } ) ;
0 commit comments