1- const computerChoiceDisplay = document . getElementById ( ' computer-choice' ) ;
2- const userChoiceDisplay = document . getElementById ( ' user-choice' ) ;
3- const resultDisplay = document . getElementById ( ' winner' ) ;
4- const possibleChoices = document . querySelectorAll ( ' .button-container > img' ) ;
1+ const computerChoiceDisplay = document . getElementById ( " computer-choice" ) ;
2+ const userChoiceDisplay = document . getElementById ( " user-choice" ) ;
3+ const resultDisplay = document . getElementById ( " winner" ) ;
4+ const possibleChoices = document . querySelectorAll ( " .button-container > img" ) ;
55
66let userChoice ;
77let computerChoice ;
88let result ;
9+ let score = {
10+ wins : 0 ,
11+ losses : 0 ,
12+ ties : 0 ,
13+ } ;
914
10- possibleChoices . forEach ( possibleChoice => possibleChoice . addEventListener ( 'click' , ( e ) => {
11- userChoice = e . target . alt ;
12- userChoiceDisplay . src = e . target . src ;
13- generateComputerChoice ( ) ;
14- getResult ( ) ;
15- } ) ) ;
15+ possibleChoices . forEach ( ( possibleChoice ) =>
16+ possibleChoice . addEventListener ( "click" , ( e ) => {
17+ userChoice = e . target . alt ;
18+ userChoiceDisplay . src = e . target . src ;
19+ generateComputerChoice ( ) ;
20+ getResult ( ) ;
21+ } )
22+ ) ;
1623
1724function generateComputerChoice ( ) {
1825 const randomNumber = Math . floor ( Math . random ( ) * possibleChoices . length ) ;
@@ -27,17 +34,29 @@ function getResult() {
2734 resultDisplay . innerHTML = result ;
2835 resultDisplay . className = "it-s-a-draw" ;
2936 } else if (
30- ( computerChoice === ' Rock' && userChoice === ' Paper' ) ||
31- ( computerChoice === ' Scissors' && userChoice === ' Rock' ) ||
32- ( computerChoice === ' Paper' && userChoice === ' Scissors' )
37+ ( computerChoice === " Rock" && userChoice === " Paper" ) ||
38+ ( computerChoice === " Scissors" && userChoice === " Rock" ) ||
39+ ( computerChoice === " Paper" && userChoice === " Scissors" )
3340 ) {
34- result = ' You win!' ;
35- resultDisplay . innerHTML = `<img src="./public/gamer.png" alt="Gamer" style="width: 10%;">` ;
36- resultDisplay . className = "you-win" ;
41+ result = " You win!" ;
42+ resultDisplay . innerHTML = `<img src="./public/gamer.png" alt="Gamer" style="width: 10%;">` ;
43+ resultDisplay . className = "you-win" ;
3744 } else {
38- result = ' You lose!' ;
39- resultDisplay . innerHTML = `<img src="./public/computer.png" alt="Computer" style="width: 10%;">` ;
40- resultDisplay . className = "you-lose" ;
45+ result = " You lose!" ;
46+ resultDisplay . innerHTML = `<img src="./public/computer.png" alt="Computer" style="width: 10%;">` ;
47+ resultDisplay . className = "you-lose" ;
4148 }
49+ if ( result === "You win!" ) {
50+ score . wins += 1 ;
51+ } else if ( result === "You lose!" ) {
52+ score . losses += 1 ;
53+ } else if ( result === "It's a draw" ) {
54+ score . ties += 1 ;
55+ }
56+ scoreElement ( ) ;
57+ }
58+ function scoreElement ( ) {
59+ document . querySelector (
60+ ".js-score"
61+ ) . innerHTML = `Wins: ${ score . wins } , Losses: ${ score . losses } , Ties: ${ score . ties } .` ;
4262}
43-
0 commit comments