1
- // Write your JavaScript code here.
2
- // Remember to pay attention to page loading!
1
+ function init ( ) {
2
+ const takeoffButton = document . getElementById ( "takeoff" ) ;
3
+ const landingButton = document . getElementById ( "landing" ) ;
4
+ const missionAbortButton = document . getElementById ( "missionAbort" ) ;
5
+ //directions
6
+ const upButton = document . getElementById ( "up" ) ;
7
+ const downButton = document . getElementById ( "down" ) ;
8
+ const leftButton = document . getElementById ( "left" ) ;
9
+ const rightButton = document . getElementById ( "right" ) ;
10
+
11
+ takeoffButton . addEventListener ( "click" , function ( ) {
12
+ const confirmation = confirm ( 'Are you ready for take off?' ) ;
13
+ if ( confirmation ) {
14
+ const flightStatusParagraph = document . getElementById ( 'flightStatus' ) ;
15
+ const shuttleBackground = document . getElementById ( 'shuttleBackground' ) ;
16
+ const spaceShuttleHeight = document . getElementById ( 'spaceShuttleHeight' ) ;
17
+
18
+ flightStatusParagraph . textContent ( "Shuttle in flight" ) ;
19
+ shuttleBackground . style . backgroundColor = 'blue' ;
20
+ spaceShuttleHeight . textContent = parseInt ( spaceShuttleHeight . textContent ) + 10000 ;
21
+
22
+ }
23
+ } ) ;
24
+ landingButton . addEventListener ( "click" , function ( ) {
25
+ alert ( 'The shuttle is landing. Landing gear engaged.' ) ;
26
+ const flightStatusParagraph = document . getElementById ( 'flightStatus' ) ;
27
+ const shuttleBackground = document . getElementById ( 'shuttleBackground' ) ;
28
+ const spaceShuttleHeight = document . getElementById ( 'spaceShuttleHeight' ) ;
29
+ flightStatusParagraph . textContent = 'The shuttle has landed' ;
30
+ shuttleBackground . style . background = 'green' ;
31
+ spaceShuttleHeight . textContent = 0 ;
32
+ } ) ;
33
+ missionAbortButton . addEventListener ( "click" , function ( ) {
34
+ const confirmation = confirm ( 'Do you want to abort the mission?' ) ;
35
+ if ( confirmation ) {
36
+ const flightStatusParagraph = document . getElementById ( 'flightStatus' ) ;
37
+ const shuttleBackground = document . getElementById ( 'shuttleBackground' ) ;
38
+ const spaceShuttleHeight = document . getElementById ( 'spaceShuttleHeight' ) ;
39
+
40
+ flightStatusParagraph . textContent = 'Mission aborted' ;
41
+ shuttleBackground . style . background = 'green' ;
42
+ spaceShuttleHeight . textContent = 0 ;
43
+
44
+ }
45
+
46
+ } ) ;
47
+ // changingd direction
48
+
49
+ upButton . addEventListener ( "click" , function ( ) {
50
+ moveRocket ( "up" ) ;
51
+ spaceShuttleHeight . textContent = parseInt ( spaceShuttleHeight . textContent ) + 10000 ;
52
+
53
+ } ) ;
54
+
55
+ downButton . addEventListener ( "click" , function ( ) {
56
+ moveRocket ( "down" ) ;
57
+ spaceShuttleHeight . textContent = parseInt ( spaceShuttleHeight . textContent ) - 10000 ;
58
+
59
+ } ) ;
60
+
61
+ leftButton . addEventListener ( "click" , function ( ) {
62
+ moveRocket ( "right" ) ;
63
+ } ) ;
64
+
65
+ rightButton . addEventListener ( "click" , function ( ) {
66
+ moveRocket ( "left" ) ;
67
+ } ) ;
68
+
69
+ //function to move rocket image
70
+ function moveRocket ( direction ) {
71
+ const rocket = document . getElementById ( "rocket" ) ;
72
+ const shuttleHeight = parseInt ( spaceShuttleHeight . textContent ) ;
73
+ switch ( direction ) {
74
+ case "up" :
75
+ shuttleBackground . style . top = ( parseInt ( shuttleBackground . style . top ) - 10 ) + "px" ;
76
+ break ;
77
+
78
+ case "down" :
79
+ shuttleBackground . style . top = ( parseInt ( shuttleBackground . style . top ) + 10 ) + "px" ;
80
+ break ;
81
+
82
+ case "right" :
83
+ shuttleBackground . style . left = ( parseInt ( shuttleBackground . style . left ) + 10 ) + "px" ;
84
+ break ;
85
+
86
+ case "left" :
87
+ shuttleBackground . style . left = ( parseInt ( shuttleBackground . style . left ) - 10 ) + "px" ;
88
+ break ;
89
+
90
+ }
91
+ }
92
+ }
93
+ window . addEventListener ( "load" , function ( ) {
94
+ console . log ( 'window loaded' ) ;
95
+ init ( ) ;
96
+
97
+
98
+
99
+ } ) ;
0 commit comments