@@ -12,13 +12,17 @@ function init () {
12
12
const buttonLeft = this . document . getElementById ( "left" ) ;
13
13
const rocket = this . document . getElementById ( "rocket" ) , left = 0 ;
14
14
15
-
16
-
17
15
let flightStatus = this . document . getElementById ( "flightStatus" )
18
16
let shuttleBackgroundColor = this . document . getElementById ( "shuttleBackground" ) ;
19
17
let shuttleHeightMiles = this . document . getElementById ( "spaceShuttleHeight" ) ;
18
+ let currentPosition = 0 ;
19
+ let currentShuttleHeight = 0 ;
20
+
21
+ rocket . style . left = "0" ;
22
+ rocket . style . position = "absolute" ;
20
23
21
24
25
+ // When "Take off" button is clicked
22
26
buttonTakeOff . addEventListener ( "click" , function ( event ) {
23
27
let response = window . confirm ( "Confirm that the shuttle is ready for takeoff." ) ;
24
28
@@ -30,16 +34,19 @@ function init () {
30
34
} ) ;
31
35
32
36
37
+ // When "Land" button is clicked
33
38
buttonLanding . addEventListener ( "click" , function ( event ) {
34
39
window . alert ( "The shuttle is landing. Landing gear engaged." ) ;
35
40
36
41
flightStatus . innerHTML = "The shuttle has landed." ;
37
42
shuttleBackgroundColor . style . backgroundColor = "green" ;
38
43
shuttleHeightMiles . innerHTML = "0" ;
39
44
45
+
40
46
} ) ;
41
47
42
48
49
+ // When "Abort Mission" button is clicked
43
50
buttonMissionAbort . addEventListener ( "click" , function ( event ) {
44
51
let response = window . confirm ( "Confirm that you want to abort the mission." ) ;
45
52
@@ -50,31 +57,51 @@ function init () {
50
57
}
51
58
} ) ;
52
59
53
-
60
+
61
+ // When "Right" button is clicked
54
62
buttonRight . addEventListener ( "click" , function ( event ) {
55
63
56
- rocket . style . position = "absolute" ;
57
- let currentPosition = parseInt ( rocket . style . left ) ;
58
-
59
- rocket . style . left = currentPosition + 10 ;
60
-
64
+ currentPosition = parseInt ( rocket . style . left ) ;
65
+ rocket . style . left = ( currentPosition + 10 ) + "px" ;
61
66
62
- console . log ( typeof rocket . style . position ) ;
67
+ console . log ( rocket . style . left ) ; // Used for testing output
68
+ } ) ;
63
69
64
- console . log ( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ) ;
65
70
66
- console . log ( rocket . style . left ) ;
67
- console . log ( typeof rocket . style . left ) ;
71
+ // When "Left" button is clicked
72
+ buttonLeft . addEventListener ( "click" , function ( event ) {
68
73
69
- // rocket.setAttribute("right", rightPosition );
74
+ currentPosition = parseInt ( rocket . style . left ) ;
70
75
76
+ if ( currentPosition > 0 ) {
77
+ rocket . style . left = ( currentPosition - 10 ) + "px" ;
78
+ } else {
79
+ console . log ( "Unable to move LEFT. Try again!" ) ;
80
+ }
81
+
82
+ console . log ( rocket . style . left ) ;
71
83
} ) ;
72
84
73
85
86
+ // When "Up" button is clicked
87
+ buttonUp . addEventListener ( "click" , function ( event ) {
74
88
89
+ currentShuttleHeight = parseInt ( shuttleHeightMiles . innerHTML ) ;
90
+ shuttleHeightMiles . innerHTML = currentShuttleHeight + 10000 ;
91
+
92
+ } ) ;
75
93
76
94
77
-
95
+ // When "Down" button is clicked
96
+ buttonDown . addEventListener ( "click" , function ( event ) {
97
+ currentShuttleHeight = parseInt ( shuttleHeightMiles . innerHTML ) ;
98
+
99
+ if ( currentShuttleHeight > 10 ) {
100
+ shuttleHeightMiles . innerHTML = currentShuttleHeight - 10000 ;
101
+ } else {
102
+ console . log ( "Unable to move DOWN, Try again!" ) ;
103
+ }
104
+ } ) ;
78
105
} ;
79
106
80
107
0 commit comments