11const 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' ) ;
44
55/**
66 * The game's settings.
@@ -38,7 +38,7 @@ const newGame = () => {
3838 // we want to hide the Start Game button after clicking it. That code will go here.
3939
4040 // 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 ) {
4242 if ( node . getAttribute ( "data-listener" ) !== "true" ) {
4343 node . addEventListener ( "click" , ( e ) => {
4444 if ( gameConfig . currentPat . length > 0 && ! gameConfig . turnInProgress ) {
@@ -64,7 +64,7 @@ const nextTurn = () => {
6464 gameConfig . playerInput = [ ] ;
6565 gameConfig . currentPat = [ ] ;
6666
67- for ( let n = 0 ; n < gameConfig . patternCount ; n ++ ) {
67+ for ( let patternCount = 0 ; patternCount < gameConfig . patternCount ; patternCount ++ ) {
6868 let patternBit = Math . floor ( Math . random ( ) * gameConfig . buttons . length ) ;
6969 gameConfig . currentPat . push ( gameConfig . buttons [ patternBit ] ) ;
7070 } ;
@@ -73,14 +73,14 @@ const nextTurn = () => {
7373} ;
7474
7575/**
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 .
7878 */
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" ) ;
8181 setTimeout ( ( ) => {
82- document . getElementById ( node ) . classList . remove ( "light" , "clicked" ) ;
83- } , gameConfig . lightTime ) ;
82+ document . querySelector ( `# ${ node } ` ) . classList . remove ( "light" , "clicked" ) ;
83+ } , time ) ;
8484} ;
8585
8686/**
@@ -90,7 +90,7 @@ const showTurn = () => {
9090 gameConfig . turnInProgress = true ;
9191 let seqPlace = 0 ;
9292 let sequence = setInterval ( ( ) => {
93- lightUp ( gameConfig . currentPat [ seqPlace ] ) ;
93+ clickNode ( gameConfig . currentPat [ seqPlace ] , gameConfig . lightTime ) ;
9494 seqPlace ++ ;
9595 if ( seqPlace >= gameConfig . currentPat . length ) {
9696 clearInterval ( sequence ) ;
@@ -99,17 +99,6 @@ const showTurn = () => {
9999 } , gameConfig . turnTime ) ;
100100} ;
101101
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-
113102/**
114103 * Increase the pattern length every 4th pattern, to a maximum of 8.
115104 */
@@ -153,11 +142,11 @@ const playerTurn = () => {
153142 } ;
154143 } else {
155144 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 ;
158147 } ;
159- if ( maxLength . innerHTML < gameConfig . patternCount ) {
160- maxLength . innerHTML = gameConfig . patternCount ;
148+ if ( maxLengthRef . innerHTML < gameConfig . patternCount ) {
149+ maxLengthRef . innerHTML = gameConfig . patternCount ;
161150 } ;
162151 gameReset ( ) ;
163152 // we want code to resummon the start button.
@@ -173,4 +162,4 @@ module.exports = {
173162 lengthUp,
174163 speedUp,
175164 playerTurn
176- } ; // testing, testing...
165+ } ;
0 commit comments