-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
62 lines (55 loc) · 1.66 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
var you;
var yourScore = 0;
var opponent;
var opponentScore = 0;
var choices = ["rock", "paper", "scissors"];
window.onload = function() {
for (let i = 0; i < 3; i++) {
// <img id="rock" src="rock.png">
let choice = document.createElement("img");
choice.id = choices[i];
choice.src = choices[i] + ".png";
choice.addEventListener("click", selectChoice);
document.getElementById("choices").append(choice);
}
}
function selectChoice() {
you = this.id;
document.getElementById("your-choice").src = you + ".png";
//random for opponent
opponent = choices[Math.floor(Math.random() *3)]; //0- .999999 * 3 =0-2.99999
document.getElementById("opponent-choice").src = opponent + ".png";
//check for winner
if (you == opponent) {
yourScore += 1;
opponentScore += 1;
}
else {
if (you == "rock") {
if (opponent == "scissors") {
yourScore += 1;
}
else if (opponent == "paper") {
opponentScore += 1;
}
}
else if (you == "scissors") {
if (opponent == "paper") {
yourScore += 1;
}
else if (opponent == "rock") {
opponentScore += 1;
}
}
else if (you == "paper") {
if (opponent == "rock") {
yourScore += 1;
}
else if (opponent == "scissors") {
opponentScore += 1;
}
}
}
document.getElementById("your-score").innerText = yourScore;
document.getElementById("opponent-score").innerText = opponentScore;
}