1
+ let height , firOpt , secOpt ;
2
+
3
+ //Gets the height input and places the value of it in the blue box
4
+ document . getElementById ( 'heightInput' ) . addEventListener ( 'input' , getHeightInput ) ;
5
+
6
+ function getHeightInput ( e ) {
7
+ document . getElementById ( 'output' ) . style . visibility = 'visible' ;
8
+ document . getElementById ( 'resetButton' ) . style . visibility = 'visible' ;
9
+ height = e . target . value ;
10
+ document . getElementById ( 'heightOutput' ) . innerHTML = height ; //placing the value to the DOM
11
+ heightConverter ( ) ;
12
+ }
13
+
14
+ document . getElementById ( 'firOpt' ) . addEventListener ( 'change' , getfirOpt ) ;
15
+
16
+ function getfirOpt ( e ) {
17
+ firOpt = e . target . value ; //Gets placeholder value
18
+ document . getElementById ( 'heightInput' ) . placeholder = 'Enter ' + firOpt + '...' ;
19
+ document . getElementById ( 'heightName' ) . innerHTML = firOpt + ":" ;
20
+ heightConverter ( ) ;
21
+ }
22
+
23
+ document . getElementById ( 'secOpt' ) . addEventListener ( 'change' , getsecOpt ) ;
24
+
25
+ function getsecOpt ( e ) {
26
+ secOpt = e . target . value ;
27
+ document . getElementById ( 'convertedHeightName' ) . innerHTML = secOpt + ":" ;
28
+ heightConverter ( ) ;
29
+ }
30
+
31
+ function heightConverter ( ) {
32
+ const finalOutput = document . querySelector ( '#finalOutput' ) ;
33
+ //Validates for Miles
34
+ if ( firOpt == "Miles" && secOpt == "Centimeters" ) {
35
+ finalOutput . innerHTML = height * 160934 ;
36
+ } else if ( firOpt == "Miles" && secOpt == "Kilometers" ) {
37
+ finalOutput . innerHTML = height * 1.60934 ;
38
+ } else if ( firOpt == "Miles" && secOpt == "Meters" ) {
39
+ finalOutput . innerHTML = height * 1609.34 ;
40
+ } else if ( firOpt == "Miles" && secOpt == "Inches" ) {
41
+ finalOutput . innerHTML = height * 63360 ;
42
+ }
43
+ //Validates for Centimeters
44
+ else if ( firOpt == "Centimeters" && secOpt == "Miles" ) {
45
+ finalOutput . innerHTML = height / 160934 ;
46
+ } else if ( firOpt == "Centimeters" && secOpt == "Kilometers" ) {
47
+ finalOutput . innerHTML = height / 100000 ;
48
+ } else if ( firOpt == "Centimeters" && secOpt == "Meters" ) {
49
+ finalOutput . innerHTML = height * 0.01 ;
50
+ } else if ( firOpt == "Centimeters" && secOpt == "Inches" ) {
51
+ finalOutput . innerHTML = height * 0.3937 ;
52
+ }
53
+ //Validates for Kilometers
54
+ else if ( firOpt == "Kilometers" && secOpt == "Centimeters" ) {
55
+ finalOutput . innerHTML = height * 100000 ;
56
+ } else if ( firOpt == "Kilometers" && secOpt == "Miles" ) {
57
+ finalOutput . innerHTML = height / 1.609 ;
58
+ } else if ( firOpt == "Kilometers" && secOpt == "Meters" ) {
59
+ finalOutput . innerHTML = height * 1000 ;
60
+ } else if ( firOpt == "Kilometers" && secOpt == "Inches" ) {
61
+ finalOutput . innerHTML = height * 39370 ;
62
+ }
63
+ //Validates for Milligram
64
+ else if ( firOpt == "Meters" && secOpt == "Miles" ) {
65
+ finalOutput . innerHTML = height / 1609 ;
66
+ } else if ( firOpt == "Meters" && secOpt == "Kilometers" ) {
67
+ finalOutput . innerHTML = height / 1000 ;
68
+ } else if ( firOpt == "Meters" && secOpt == "Centimeters" ) {
69
+ finalOutput . innerHTML = height * 100 ;
70
+ } else if ( firOpt == "Meters" && secOpt == "Inches" ) {
71
+ finalOutput . innerHTML = height * 39.37 ;
72
+ }
73
+
74
+ //Validates for Inches
75
+ else if ( firOpt == "Inches" && secOpt == "Miles" ) {
76
+ finalOutput . innerHTML = height / 63360 ;
77
+ } else if ( firOpt == "Inches" && secOpt == "Kilometers" ) {
78
+ finalOutput . innerHTML = height / 39370 ;
79
+ } else if ( firOpt == "Inches" && secOpt == "Meters" ) {
80
+ finalOutput . innerHTML = height / 39.37 ;
81
+ } else if ( firOpt == "Inches" && secOpt == "Centimeters" ) {
82
+ finalOutput . innerHTML = height * 2.54 ;
83
+ }
84
+ //Validates if first option is the same as second option
85
+ else {
86
+ finalOutput . innerHTML = height ;
87
+ }
88
+ }
89
+
90
+ document . getElementById ( 'resetButton' ) . addEventListener ( 'click' , reset ) ;
91
+
92
+ function reset ( ) {
93
+ document . getElementById ( 'mainForm' ) . reset ( ) ;
94
+ document . getElementById ( 'output' ) . style . visibility = 'hidden' ;
95
+ document . getElementById ( 'resetButton' ) . style . visibility = 'hidden' ;
96
+ }
0 commit comments