-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
120 lines (103 loc) · 3.09 KB
/
script.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 userChoice = "";
let computerChoice = "";
const choices = document.querySelector("#choices");
const results = document.querySelector("#results");
const resultsP = document.createElement('p');
const btnRock = document.createElement('button');
btnRock.textContent = "ROCK";
const btnPaper = document.createElement('button');
btnPaper.textContent = "PAPER";
const btnScissor = document.createElement('button');
btnScissor.textContent = "SCISSOR";
const score = document.createElement('div');
const scoreHead = document.createElement('h3');
const scoreNumber = document.createElement('p');
let userScore = 0;
let computerScore = 0;
scoreHead.textContent = "Scoreboard";
score.appendChild(scoreHead);
score.appendChild(scoreNumber);
results.appendChild(resultsP);
results.appendChild(score);
btnRock.addEventListener("click", () => {
userChoice = "rock";
playRound();
});
btnPaper.addEventListener("click", () => {
userChoice = "paper";
playRound();
});
btnScissor.addEventListener("click", () => {
userChoice = "scissor";
playRound();
});
choices.appendChild(btnRock);
choices.appendChild(btnPaper);
choices.appendChild(btnScissor);
function getComputerChoice() {
computerChoice = Math.floor(Math.random() * 3); //returns a random number, 0, 1, or 2
if (computerChoice == 0) {
return "rock";
}
else if (computerChoice == 1) {
return "paper";
}
else {
return "scissor";
}
}
function playRound() {
computerChoice = getComputerChoice();
if (userChoice == "rock") {
if (computerChoice == "rock") {
resultsP.textContent = "It's a tie!";
}
else if (computerChoice == "paper") {
resultsP.textContent = "You lose! Paper beats rock!";
computerScore++;
}
else {
resultsP.textContent = "You win!, Rock beats scissors!";
userScore++;
}
}
else if (userChoice == "paper") {
if (computerChoice == "paper") {
resultsP.textContent = "It's a tie!";
}
else if (computerChoice == "rock") {
resultsP.textContent = "You win! Paper beats rock!";
userScore++;
}
else {
resultsP.textContent = "You lose!, Scissors beats paper!";
computerScore++;
}
}
else {
if (computerChoice == "rock") {
resultsP.textContent = "You lose! Paper beats rock!";
computerScore++;
}
else if (computerChoice == "paper") {
resultsP.textContent = "You win!, Scissors beats paper!";
userScore++;
}
else {
resultsP.textContent = "It's a tie!";
}
}
if(userScore == 5) {
scoreNumber.textContent = "You are the winner!";
computerScore = 0;
userScore = 0;
}
else if(computerScore == 5) {
scoreNumber.textContent = "You are the winner!";
userScore = 0;
computerScore = 0;
} else {
scoreNumber.textContent = `You: ${userScore} Computer: ${computerScore}`;
}
}
// playRound();