-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (96 loc) · 4.17 KB
/
index.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
// PLAYER ELEMENTS
const rock = document.querySelector('#rock');
const paper = document.querySelector('#paper');
const scissors = document.querySelector('#scissors');
// RESULTS AND RESTART SECTIONS
const results = document.querySelector(".display-result");
const restart = document.querySelector('.restart-button');
rock.addEventListener('click', playRound);
paper.addEventListener('click', playRound);
scissors.addEventListener('click', playRound);
restart.addEventListener('click', game);
// Declare function computerPlay
function computerPlay() {
const number = Math.floor(Math.random() * 1000);
if (number % 3 === 0) {
return "rock";
}
if (number % 3 === 1) {
return "paper";
}
return "scissors";
}
// Call function computerPlay
computerPlay();
let playerScore = 0;
let computerScore = 0;
// Declare function playRound with with playerSelection and computerSelection
function playRound(playerSelection, computerSelection) {
playerSelection = this.dataset.button
playerSelection = playerSelection.toUpperCase();
computerSelection = computerPlay().toUpperCase();
const resultPara = document.createElement("p");
results.prepend(resultPara);
const roundWinner = document.createElement('p');
results.prepend(roundWinner);
const finalWinner = document.createElement("p");
results.prepend(finalWinner);
finalWinner.style.fontSize = '2rem';
finalWinner.style.fontWeight = '900';
if (playerSelection === 'ROCK' && computerSelection === 'PAPER') {
computerScore++;
roundWinner.textContent += `${computerSelection} beats ${playerSelection}
(Player Score: ${playerScore}, Computer Score: ${computerScore})`
}
else if (playerSelection === 'ROCK' && computerSelection === 'SCISSORS') {
playerScore++;
roundWinner.textContent += `${playerSelection} beats ${computerSelection}
(Player Score: ${playerScore}, Computer Score: ${computerScore})`;
}
else if (playerSelection === 'PAPER' && computerSelection === 'ROCK') {
playerScore++;
roundWinner.textContent += `${playerSelection} beats ${computerSelection}
(Player Score: ${playerScore}, Computer Score: ${computerScore})`;
}
else if (playerSelection === 'PAPER' && computerSelection === 'SCISSORS') {
computerScore++;
roundWinner.textContent += `${computerSelection} beats ${playerSelection}
(Player Score: ${playerScore}, Computer Score: ${computerScore})`;
}
else if (playerSelection === 'SCISSORS' && computerSelection === 'ROCK') {
computerScore++;
roundWinner.textContent += `${computerSelection} beats ${playerSelection}
(Player Score: ${playerScore}, Computer Score: ${computerScore})`;
}
else if (playerSelection === 'SCISSORS' && computerSelection === 'PAPER') {
playerScore++;
roundWinner.textContent += `${playerSelection} beats ${computerSelection}
(Player Score: ${playerScore}, Computer Score: ${computerScore})`;
}
else {
resultPara.textContent += `Player selects ${playerSelection} and computer selects ${computerSelection}:`
roundWinner.textContent += `Tie!
(Player Score: ${playerScore}, Computer Score: ${computerScore})`; // displays tie
}
// CHECKS NUMBER OF ROUNDS IN PROPORTION TO WINS.
if (computerScore > playerScore && computerScore >= 5) {
finalWinner.textContent = `Computer Wins and You Lose!`;
restart.style.display = "block";
console.log("Computer Wins and You Lose!");
rock.removeEventListener("click", playRound);
paper.removeEventListener("click", playRound);
scissors.removeEventListener("click", playRound);
} else if (playerScore > computerScore && playerScore >= 5) {
finalWinner.textContent = `You Win and Computer Loses!`;
restart.style.display = "block";
console.log("You Win and Computer Loses!");
rock.removeEventListener("click", playRound);
paper.removeEventListener("click", playRound);
scissors.removeEventListener("click", playRound);
}
}
// RESTARTS GAME BY RELOADING THE PAGE
function game() {
window.location.reload('true');
}
// TODO Refactor using the following implementations from https://discord.com/channels/735923219315425401/763129518780842016/852248588720668722