-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
120 lines (96 loc) · 4.62 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
let playerPoints=0;
let computerPoints=0;
function getComputerChoice() {
const randomOption = Math.floor(Math.random()*3);
if (randomOption === 0) {
return('Rock');
}
else if (randomOption === 1) {
return('Paper');
}
else if (randomOption === 2) {
return('Scissors');
}
}
function playRound(playerSelection, computerSelection) {
if (playerSelection.toLowerCase() == 'rock' && computerSelection == 'Paper') {
++computerPoints
let message = 'You Lose! Paper beats rock!';
return(message);
}
else if (playerSelection.toLowerCase() == 'paper' && computerSelection == 'Scissors') {
++computerPoints
let message = 'You Lose! Scissors beats paper!';
return(message);
}
else if (playerSelection.toLowerCase() == 'scissors' & computerSelection == 'Rock') {
++computerPoints
let message = 'You Lose! Rock beats scissors!';
return(message);
}
else if (playerSelection.toLowerCase() == 'rock' && computerSelection == 'Scissors') {
++playerPoints;
let message = 'You Win! Rock beats scissors!';
return(message);
}
else if (playerSelection.toLowerCase() == 'paper' && computerSelection == 'Rock') {
++playerPoints;
let message = 'You Win!! Paper beats rock!';
return(message);
}
else if (playerSelection.toLowerCase() == 'scissors' && computerSelection == 'Paper') {
++playerPoints;
let message = 'You Win! Scissors beats paper!';
return(message);
}
else if (playerSelection.toLowerCase() == computerSelection.toLowerCase()) {
let message = 'Draw!';
return(message);
}
}
function game() {
const round = document.querySelector('#round');
const scores = document.querySelector('#scores');
const win = document.querySelector('#win');
const rock = document.querySelector('#rock');
rock.addEventListener('click', () => {
let computerSelection = getComputerChoice();
round.textContent = playRound('rock', computerSelection);
scores.textContent = gameScore(playerPoints, computerPoints);
win.textContent = checkForWin(playerPoints, computerPoints);
});
const paper = document.querySelector('#paper');
paper.addEventListener('click', () => {
let computerSelection = getComputerChoice();
round.textContent = playRound('paper', computerSelection);
scores.textContent = gameScore(playerPoints, computerPoints);
win.textContent = checkForWin(playerPoints, computerPoints);
});
const scissors = document.querySelector('#scissors');
scissors.addEventListener('click', () => {
let computerSelection = getComputerChoice();
round.textContent = playRound('scissors', computerSelection);
scores.textContent = gameScore(playerPoints, computerPoints);
win.textContent = checkForWin(playerPoints, computerPoints);
});
const restart = document.querySelector('#restart')
restart.addEventListener('click', () => {
playerPoints = 0;
computerPoints = 0;
round.textContent = "";
scores.textContent = gameScore(playerPoints, computerPoints)
win.textContent = "";
});
}
function gameScore (playerScore, computerScore) {
return("Your score: " + playerScore + " " + "Computer score: " + computerScore)
}
function checkForWin (playerScore, computerScore) {
if (playerScore >= 5) {
return("You've won the game! Would you like to play again?")
}
else if (computerScore >= 5) {
return("You've lost the game! Would you like to play again?")
}
}
game();