-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
130 lines (110 loc) · 3.35 KB
/
javascript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// return computer's choice
function computerPlay() {
let random = Math.floor((Math.random() * 3) + 1);
let play;
if (random === 1) {
play = 'Rock';
} else if (random === 2) {
play = 'Paper';
} else {
play = 'Scissor';
}
return play;
}
// play a round
function playRound(playerSelection, computerSelection) {
playerSelection = playerSelection.toUpperCase();
let result;
if (playerSelection === 'ROCK') {
switch (computerSelection) {
case 'Rock':
result = 'Draw!';
break;
case 'Paper':
result = 'Player Lose!';
break;
case 'Scissor':
result = 'Player Win!';
break;
}
}
if (playerSelection === 'PAPER') {
switch (computerSelection) {
case 'Rock':
result = 'Player Win!';
break;
case 'Paper':
result = 'Draw!';
break;
case 'Scissor':
result = 'Player Lose!';
break;
}
}
if (playerSelection === 'SCISSOR') {
switch (computerSelection) {
case 'Rock':
result = 'Player Lose!';
break;
case 'Paper':
result = 'Player Win!';
break;
case 'Scissor':
result = 'Draw!';
break;
}
}
return result;
}
// play 5 rounds
function game() {
// let playerSelection;
// let computerSelection;
// let round_result;
const rock_btn = document.querySelector('#rock-btn');
const paper_btn = document.querySelector('#paper-btn');
const scissor_btn = document.querySelector('#scissor-btn');
rock_btn.addEventListener('click', rockButton);
paper_btn.addEventListener('click', paperButton);
scissor_btn.addEventListener('click', scissorButton);
function rockButton() {
let playerSelection = 'rock';
let computerSelection = computerPlay();
let value = playRound(playerSelection, computerSelection);
scoring(value);
}
function paperButton() {
let playerSelection = 'paper';
let computerSelection = computerPlay();
let value = playRound(playerSelection, computerSelection);
scoring(value);
}
function scissorButton() {
let playerSelection = 'scissor';
let computerSelection = computerPlay();
let value = playRound(playerSelection, computerSelection);
scoring(value);
}
}
function scoring(value) {
let playerScore = 0;
let computerScore = 0;
let player_score = document.querySelector('#player_score');
let computer_score = document.querySelector('#computer_score');
const final_result = document.querySelector('#final_result');
if (value === 'Player Win!') {
playerScore++;
} else if (value === 'Player Lose!') {
computerScore++;
}
player_score.textContent = playerScore;
computer_score.textContent = computerScore;
if (playerScore > computerScore) {
final_result.textContent = 'You win the game!';
} else if (playerScore < computerScore) {
final_result.textContent = 'Computer wins the game!';
} else {
final_result.textContent = 'It\'s a tie!';
}
}
game();