1
1
console . log ( "hi" ) ;
2
2
3
- const p1Button = document . querySelector ( '.p1button' ) ;
4
- const p2Button = document . querySelector ( '.p2button' ) ;
5
- const rstButton = document . querySelector ( '.reset' ) ;
6
-
7
- const p1Display = document . querySelector ( '.p1scrdisp' ) ;
8
- const p2Display = document . querySelector ( '.p2scrdisp' ) ;
9
-
10
- const winningScore = document . querySelector ( '.limit' ) ;
11
- const body = document . querySelector ( 'body' ) ;
12
-
13
- let p1Score = 0 ;
14
- let p2Score = 0 ;
15
- let limit = 0 ;
16
- let isGameOver = false ;
17
-
3
+ const p1Button = document . querySelector ( '.p1button' ) ;
4
+ const p2Button = document . querySelector ( '.p2button' ) ;
5
+ const rstButton = document . querySelector ( '.reset' ) ;
6
+
7
+ const p1Display = document . querySelector ( '.p1scrdisp' ) ;
8
+ const p2Display = document . querySelector ( '.p2scrdisp' ) ;
9
+
10
+ const winningScore = document . querySelector ( '.limit' ) ;
11
+ const body = document . querySelector ( 'body' ) ;
12
+
13
+ let p1Score = 0 ;
14
+ let p2Score = 0 ;
15
+ let limit = 0 ;
16
+ let isGameOver = false ;
17
+
18
18
// player 1 Score update
19
19
20
- p1Button . addEventListener ( 'click' , function ( evt ) {
21
- console . log ( "You pressed player 1" ) ;
22
-
23
- if ( isGameOver !== true && limit !== 0 )
24
- {
25
- p1Score += 1 ;
26
-
27
- if ( p1Score == limit )
28
- {
29
- isGameOver = true ;
30
- p1Display . classList . add ( 'winner' ) ;
31
- p2Display . classList . add ( 'loser' ) ;
32
- p1Button . style . background = '#20c997' ;
33
- p2Button . style . background = '#f03e3e' ;
34
- alert ( "PLAYER 1 WON!!" ) ;
35
- }
36
-
37
- p1Display . textContent = p1Score ;
38
- }
39
- } ) ;
40
-
41
-
42
- // Player 2 Score update
43
-
44
- p2Button . addEventListener ( 'click' , function ( evt ) {
45
- console . log ( "You pressed player 2" ) ;
46
-
47
- if ( isGameOver !== true && limit !== 0 )
48
- {
49
- p2Score += 1 ;
50
-
51
- if ( p2Score == limit )
52
- {
53
- isGameOver = true ;
54
- p2Display . classList . add ( 'winner' ) ;
55
- p1Display . classList . add ( 'loser' ) ;
56
- p2Button . style . background = '#20c997' ;
57
- p1Button . style . background = '#f03e3e' ;
58
- alert ( "PLAYER 2 WON!!" ) ;
59
- }
60
- p2Display . textContent = p2Score ;
61
- }
20
+ p1Button . addEventListener ( 'click' , function ( evt ) {
21
+ console . log ( "You pressed player 1" ) ;
22
+
23
+ if ( isGameOver !== true && limit !== 0 ) {
24
+ p1Score += 1 ;
25
+
26
+ if ( p1Score == limit ) {
27
+ isGameOver = true ;
28
+ p1Display . classList . add ( 'winner' ) ;
29
+ p2Display . classList . add ( 'loser' ) ;
30
+ p1Button . style . background = '#20c997' ;
31
+ p2Button . style . background = '#f03e3e' ;
32
+ alert ( "PLAYER 1 WON!!" ) ;
33
+ }
34
+
35
+ p1Display . textContent = p1Score ;
36
+ }
62
37
} ) ;
63
-
38
+
39
+
40
+ // Player 2 Score update
41
+
42
+ p2Button . addEventListener ( 'click' , function ( evt ) {
43
+ console . log ( "You pressed player 2" ) ;
44
+
45
+ if ( isGameOver !== true && limit !== 0 ) {
46
+ p2Score += 1 ;
47
+
48
+ if ( p2Score == limit ) {
49
+ isGameOver = true ;
50
+ p2Display . classList . add ( 'winner' ) ;
51
+ p1Display . classList . add ( 'loser' ) ;
52
+ p2Button . style . background = '#20c997' ;
53
+ p1Button . style . background = '#f03e3e' ;
54
+ alert ( "PLAYER 2 WON!!" ) ;
55
+ }
56
+ p2Display . textContent = p2Score ;
57
+ }
58
+ } ) ;
59
+
64
60
//Reseting all scores
65
61
66
62
rstButton . addEventListener ( 'click' , reset ) ;
67
-
68
- function reset ( )
69
- {
70
- isGameOver = false ;
71
- p1Score = 0 ;
72
- p2Score = 0 ;
73
- p1Display . textContent = p1Score ;
74
- p2Display . textContent = p2Score ;
75
- p1Display . classList . remove ( 'winner' , 'loser' ) ;
76
- p2Display . classList . remove ( 'loser' , 'winner' ) ;
77
-
78
- p1Button . style . background = '#adb5bd' ;
79
- p2Button . style . background = '#adb5bd' ;
80
- }
63
+
64
+ function reset ( ) {
65
+ isGameOver = false ;
66
+ p1Score = 0 ;
67
+ p2Score = 0 ;
68
+ p1Display . textContent = p1Score ;
69
+ p2Display . textContent = p2Score ;
70
+ p1Display . classList . remove ( 'winner' , 'loser' ) ;
71
+ p2Display . classList . remove ( 'loser' , 'winner' ) ;
72
+
73
+ p1Button . style . background = '#adb5bd' ;
74
+ p2Button . style . background = '#adb5bd' ;
75
+ }
81
76
82
77
// Limit set by user
83
- winningScore . addEventListener ( 'change' , ( ) => {
84
- limit = parseInt ( winningScore . value )
85
- reset ( ) ;
78
+ winningScore . addEventListener ( 'change' , ( ) => {
79
+ limit = parseInt ( winningScore . value )
80
+ reset ( ) ;
81
+
82
+ } ) ;
83
+
86
84
87
- } ) ;
0 commit comments