-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
156 lines (148 loc) · 5.78 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function getComputerChoice(){
let choice = [ "rock", "paper", "scissors"];
let answer = choice[ Math.floor(Math.random() * choice.length)];
return answer
}
let computerPoint = 0;
let playerPoint = 0;
function playRound(playerSelection, computerSelection){
if ((playerSelection === "rock") && (computerSelection === "scissors")) {
let result = document.createElement("p");
result.textContent ="You won Rock beats Scissors"
let div = document.querySelector(".results");
div.textContent = ""
div.appendChild(result)
let playerP = document.createElement("div");
playerPoint += 1
playerP.textContent = `player = ${playerPoint}`
div.appendChild(playerP)
let computerP = document.createElement("div");
computerP.textContent = `computer= ${computerPoint}`
div.appendChild(computerP);
if(playerPoint === 5){
div.textContent = "Victory!"
computerPoint = 0
playerPoint = 0
}
}
else if ((playerSelection === "rock") && (computerSelection === "paper" )) {
let result = document.createElement("p");
result.textContent ="You lost Paper beats Rock"
let div = document.querySelector(".results");
div.textContent = ""
div.appendChild(result)
let playerP = document.createElement("div");
computerPoint += 1
playerP.textContent = `player = ${playerPoint}`
div.appendChild(playerP)
let computerP = document.createElement("div");
computerP.textContent = `computer= ${computerPoint}`
div.appendChild(computerP);
if (computerPoint === 5){
div.textContent = "Defeat!"
computerPoint = 0
playerPoint = 0
}
}
else if ((playerSelection === "paper") && (computerSelection === "scissors")) {
let result = document.createElement("p");
result.textContent ="You lost Scissors beats Paper"
let div = document.querySelector(".results");
div.textContent = ""
div.appendChild(result)
let playerP = document.createElement("div");
computerPoint += 1
playerP.textContent = `player = ${playerPoint}`
div.appendChild(playerP)
let computerP = document.createElement("div");
computerP.textContent = `computer= ${computerPoint}`
div.appendChild(computerP);
if (computerPoint === 5){
div.textContent = "Defeat!"
computerPoint = 0
playerPoint = 0
}
}
else if((playerSelection === "paper") && (computerSelection === "rock")) {
let result = document.createElement("p");
result.textContent ="You won Paper beats Rock"
let div = document.querySelector(".results");
div.textContent = ""
div.appendChild(result)
let playerP = document.createElement("div");
playerPoint += 1
playerP.textContent = `player = ${playerPoint}`
div.appendChild(playerP)
let computerP = document.createElement("div")
computerP.textContent = `computer= ${computerPoint}`
div.appendChild(computerP);
if(playerPoint === 5){
div.textContent = "Victory!"
computerPoint = 0
playerPoint = 0
}
}
else if((playerSelection === "scissors") && (computerSelection === "rock")) {
let result = document.createElement("p");
result.textContent ="You lost Rock beats Scissors"
let div = document.querySelector(".results");
div.textContent = ""
div.appendChild(result)
let playerP = document.createElement("div");
computerPoint += 1
playerP.textContent = `player = ${playerPoint}`
div.appendChild(playerP)
let computerP = document.createElement("div");
computerP.textContent = `computer= ${computerPoint}`
div.appendChild(computerP)
if (computerPoint === 5){
div.textContent = "Defeat!"
computerPoint = 0
playerPoint = 0
}
}
else if((playerSelection === "scissors") && (computerSelection === "paper")) {
let result = document.createElement("p");
result.textContent ="You won Scissors beats Paper"
let div = document.querySelector(".results");
div.textContent = ""
div.appendChild(result)
let playerP = document.createElement("div");
playerPoint += 1
playerP.textContent = `player = ${playerPoint}`
div.appendChild(playerP)
let computerP = document.createElement("div");
computerP.textContent = `computer= ${computerPoint}`
div.appendChild(computerP);
if(playerPoint === 5){
div.textContent = "Victory!"
computerPoint = 0
playerPoint = 0
}
}
else {
let result = document.createElement("p");
result.textContent =" It's a tie"
let div = document.querySelector(".results");
div.textContent = ""
div.appendChild(result)
let playerP = document.createElement("div");
playerP.textContent = `player = ${playerPoint}`
div.appendChild(playerP)
let computerP = document.createElement("div");
computerP.textContent = `computer= ${computerPoint}`
div.appendChild(computerP)
};
}
const rockButton = document.getElementById("rock");
const paperButton = document.getElementById("paper");
const scissorsButton = document.getElementById("scissors");
rockButton.addEventListener("click", function () {
playRound("rock", getComputerChoice())
});
paperButton.addEventListener("click", function() {
playRound("paper", getComputerChoice())
});
scissorsButton.addEventListener("click", function() {
playRound("scissors", getComputerChoice())
});