1+ let player , cpu ;
2+
3+
4+
5+ document . getElementById ( "rock" ) . addEventListener ( "click" , ( ) => simulate ( "rock" ) ) ;
6+ document . getElementById ( "paper" ) . addEventListener ( "click" , ( ) => simulate ( "paper" ) ) ;
7+ document . getElementById ( "scissors" ) . addEventListener ( "click" , ( ) => simulate ( "scissors" ) ) ;
8+
9+
10+ function simulate ( player ) {
11+ cpu_choices = [ "rock" , "paper" , "scissors" ] ;
12+
13+ random_element = Math . floor ( Math . random ( ) * 3 ) ;
14+ cpu = cpu_choices [ random_element ] ;
15+
16+ console . log ( cpu ) ;
17+
18+ if ( player == "rock" ) {
19+ if ( cpu == "scissors" ) {
20+ document . getElementById ( "outcome" ) . textContent = "OUTCOME: Player Wins" ;
21+ document . getElementById ( "choices" ) . textContent = "ROCK BEATS SCISSORS" ;
22+ }
23+ if ( cpu == "paper" ) {
24+ document . getElementById ( "outcome" ) . textContent = "OUTCOME: Computer Wins" ;
25+ document . getElementById ( "choices" ) . textContent = "PAPER BEATS ROCK" ;
26+ }
27+ if ( cpu == "rock" ) {
28+ document . getElementById ( "outcome" ) . textContent = "OUTCOME: Tie" ;
29+ document . getElementById ( "choices" ) . textContent = "YOU BOTH PICKED ROCK" ;
30+ }
31+ }
32+ if ( player == "scissors" ) {
33+ if ( cpu == "rock" ) {
34+ document . getElementById ( "outcome" ) . textContent = "OUTCOME: Computer Wins" ;
35+ document . getElementById ( "choices" ) . textContent = "ROCK BEATS SCISSORS" ;
36+ }
37+ if ( cpu == "paper" ) {
38+ document . getElementById ( "outcome" ) . textContent = "OUTCOME: Player Wins" ;
39+ document . getElementById ( "choices" ) . textContent = "SCISSORS BEATS PAPER" ;
40+ }
41+ if ( cpu == "scissors" ) {
42+ document . getElementById ( "outcome" ) . textContent = "OUTCOME: Tie" ;
43+ document . getElementById ( "choices" ) . textContent = "YOU BOTH PICKED SCISSORS" ;
44+ }
45+ }
46+ if ( player == "paper" ) { // player is paper
47+ if ( cpu == "rock" ) {
48+ document . getElementById ( "outcome" ) . textContent = "OUTCOME: Player Wins" ;
49+ document . getElementById ( "choices" ) . textContent = "PAPER BEATS ROCK" ;
50+ }
51+ if ( cpu == "scissors" ) {
52+ document . getElementById ( "outcome" ) . textContent = "OUTCOME: Computer Wins" ;
53+ document . getElementById ( "choices" ) . textContent = "SCISSORS BEATS PAPER" ;
54+ }
55+ if ( cpu == "paper" ) {
56+ document . getElementById ( "outcome" ) . textContent = "OUTCOME: Tie" ;
57+ document . getElementById ( "choices" ) . textContent = "YOU BOTH PICKED PAPER" ;
58+ }
59+ }
60+
61+ }
0 commit comments