1+ const computerChoiceDisplay = document . getElementById ( 'computer-choice' )
2+ const playerChoiceDisplay = document . getElementById ( 'player-choice' )
3+ const resultDisplay = document . getElementById ( 'result' )
4+ const choices = document . querySelectorAll ( 'button' )
5+ let playerChoice
6+ let computerChoice
7+ let result
8+
9+ // console.log(computerChoiceDisplay)
10+ // console.log(computerChoiceDisplay.id)
11+ //console.log(choices)
12+
13+
14+
15+ console . log ( playerChoiceDisplay . innerHTML , 'before' )
16+ //listeners
17+ choices . forEach ( choice => choice . addEventListener ( 'click' , ( e ) => {
18+ playerChoice = e . target . id
19+ playerChoiceDisplay . innerHTML = playerChoice
20+
21+ createComputerChoice ( )
22+ getResult ( )
23+ //console.log(playerChoiceDisplay, 'after')
24+ } ) )
25+
26+ function createComputerChoice ( ) {
27+ const randomNumber = Math . floor ( Math . random ( ) * choices . length ) + 1 //3 +1
28+ console . log ( randomNumber )
29+
30+ if ( randomNumber === 1 ) {
31+ computerChoice = 'rock'
32+ }
33+ if ( randomNumber === 2 ) {
34+ computerChoice = 'scissors'
35+ }
36+ if ( randomNumber === 3 ) {
37+ computerChoice = 'paper'
38+ }
39+
40+ computerChoiceDisplay . innerHTML = computerChoice
41+
42+ }
43+ function getResult ( ) {
44+
45+ if ( computerChoice === playerChoice ) {
46+ result = 'We have a tie!'
47+ }
48+ if ( computerChoice === 'rock' && playerChoice === 'paper' ) {
49+ result = 'You won the game'
50+ }
51+ if ( computerChoice === 'rock' && playerChoice === 'scissors' ) {
52+ result = 'You lost the game'
53+ }
54+ if ( computerChoice === 'paper' && playerChoice === 'scissors' ) {
55+ result = 'You won the game'
56+ }
57+ if ( computerChoice === 'paper' && playerChoice === 'rock' ) {
58+ result = 'You lost the game'
59+ }
60+ if ( computerChoice === 'scissors' && playerChoice === 'rock' ) {
61+ result = 'You won the game'
62+ }
63+ if ( computerChoice === 'scissors' && playerChoice === 'paper' ) {
64+ result = 'You lost the game'
65+ }
66+
67+ resultDisplay . innerHTML = result
68+ }
69+
70+ createComputerChoice ( )
0 commit comments