-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
58 lines (52 loc) · 1.53 KB
/
Copy pathscript.js
File metadata and controls
58 lines (52 loc) · 1.53 KB
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
let you;
let yourScore = 0;
let opponent;
let opponentScore = 0;
const choices = ["rock", "paper", "scissor"];
window.onload = function() {
for(let i = 0; i < 3; i++){
let choice = document.createElement("img");
choice.id = choices[i];
choice.src = choices[i] + ".jpg";
choice.addEventListener("click",selectChoice);
document.getElementById("choices").append(choice);
}
}
function selectChoice() {
you = this.id;
document.getElementById("your-choice").src = you + ".jpg";
opponent = choices[Math.floor(Math.random() * 3)];
document.getElementById("opponent-choice").src = opponent + ".jpg";
if(you == opponent){
yourScore += 1;
opponentScore += 1;
}
else {
if (you == "rock") {
if (opponent == "scissor") {
yourScore += 1;
}
else if (opponent == "paper") {
opponentScore += 1;
}
}
else if (you == "scissor") {
if (opponent == "paper") {
yourScore += 1;
}
else if (opponent == "rock") {
opponentScore += 1;
}
}
else if (you == "paper") {
if (opponent == "rock") {
yourScore += 1;
}
else if (opponent == "scissor") {
opponentScore += 1;
}
}
}
document.getElementById("your-score").innerText = yourScore;
document.getElementById("opponent-score").innerText = opponentScore;
}