1
1
const listOfNodes = Array . from ( document . querySelectorAll ( '.node' ) ) ;
2
- const highScore = document . querySelector ( '#highscore' ) ;
3
- const maxLength = document . querySelector ( '#maxlength' ) ;
2
+ const highScoreRef = document . querySelector ( '#highscore' ) ;
3
+ const maxLengthRef = document . querySelector ( '#maxlength' ) ;
4
4
5
5
/**
6
6
* The game's settings.
@@ -38,7 +38,7 @@ const newGame = () => {
38
38
// we want to hide the Start Game button after clicking it. That code will go here.
39
39
40
40
// start the game. This entire part was copied from the Simon game codealong challenge () and edited for my purposes.
41
- for ( let node of listOfNodes ) {
41
+ for ( const node of listOfNodes ) {
42
42
if ( node . getAttribute ( "data-listener" ) !== "true" ) {
43
43
node . addEventListener ( "click" , ( e ) => {
44
44
if ( gameConfig . currentPat . length > 0 && ! gameConfig . turnInProgress ) {
@@ -64,7 +64,7 @@ const nextTurn = () => {
64
64
gameConfig . playerInput = [ ] ;
65
65
gameConfig . currentPat = [ ] ;
66
66
67
- for ( let n = 0 ; n < gameConfig . patternCount ; n ++ ) {
67
+ for ( let patternCount = 0 ; patternCount < gameConfig . patternCount ; patternCount ++ ) {
68
68
let patternBit = Math . floor ( Math . random ( ) * gameConfig . buttons . length ) ;
69
69
gameConfig . currentPat . push ( gameConfig . buttons [ patternBit ] ) ;
70
70
} ;
@@ -73,14 +73,14 @@ const nextTurn = () => {
73
73
} ;
74
74
75
75
/**
76
- * Causes buttons to light up during play.
77
- * @param node ID of the button lighting up .
76
+ * Has each button react to being clicked. Also causes buttons to light up during play.
77
+ * @param node The ID of the button being clicked .
78
78
*/
79
- const lightUp = node => {
80
- document . getElementById ( node ) . classList . add ( "light" , "clicked" ) ;
79
+ const clickNode = ( node , time = 150 ) => {
80
+ document . querySelector ( `# ${ node } ` ) . classList . add ( "light" , "clicked" ) ;
81
81
setTimeout ( ( ) => {
82
- document . getElementById ( node ) . classList . remove ( "light" , "clicked" ) ;
83
- } , gameConfig . lightTime ) ;
82
+ document . querySelector ( `# ${ node } ` ) . classList . remove ( "light" , "clicked" ) ;
83
+ } , time ) ;
84
84
} ;
85
85
86
86
/**
@@ -90,7 +90,7 @@ const showTurn = () => {
90
90
gameConfig . turnInProgress = true ;
91
91
let seqPlace = 0 ;
92
92
let sequence = setInterval ( ( ) => {
93
- lightUp ( gameConfig . currentPat [ seqPlace ] ) ;
93
+ clickNode ( gameConfig . currentPat [ seqPlace ] , gameConfig . lightTime ) ;
94
94
seqPlace ++ ;
95
95
if ( seqPlace >= gameConfig . currentPat . length ) {
96
96
clearInterval ( sequence ) ;
@@ -99,17 +99,6 @@ const showTurn = () => {
99
99
} , gameConfig . turnTime ) ;
100
100
} ;
101
101
102
- /**
103
- * Has each button react to being clicked.
104
- * @param node The ID of the button being clicked.
105
- */
106
- const clickNode = node => {
107
- document . getElementById ( node ) . classList . add ( "light" , "clicked" ) ;
108
- setTimeout ( ( ) => {
109
- document . getElementById ( node ) . classList . remove ( "light" , "clicked" ) ;
110
- } , 150 ) ;
111
- } ;
112
-
113
102
/**
114
103
* Increase the pattern length every 4th pattern, to a maximum of 8.
115
104
*/
@@ -153,11 +142,11 @@ const playerTurn = () => {
153
142
} ;
154
143
} else {
155
144
alert ( "BZZT. From the top." ) ;
156
- if ( highScore . innerHTML < gameConfig . score ) {
157
- highScore . innerHTML = gameConfig . score ;
145
+ if ( highScoreRef . innerHTML < gameConfig . score ) {
146
+ highScoreRef . innerHTML = gameConfig . score ;
158
147
} ;
159
- if ( maxLength . innerHTML < gameConfig . patternCount ) {
160
- maxLength . innerHTML = gameConfig . patternCount ;
148
+ if ( maxLengthRef . innerHTML < gameConfig . patternCount ) {
149
+ maxLengthRef . innerHTML = gameConfig . patternCount ;
161
150
} ;
162
151
gameReset ( ) ;
163
152
// we want code to resummon the start button.
@@ -173,4 +162,4 @@ module.exports = {
173
162
lengthUp,
174
163
speedUp,
175
164
playerTurn
176
- } ; // testing, testing...
165
+ } ;
0 commit comments