-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
95 lines (87 loc) · 2.52 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
const userChoiceDisplay = document.getElementById("userChoice");
const computerChoiceDisplay = document.getElementById("computerChoice");
const resultDisplay = document.getElementById("result");
const score = document.getElementById("score");
const startGame = document.getElementById("startGame");
const containerGame = document.getElementById("gameContainer");
const btns = document.querySelectorAll(".btn-choice");
let userChoice;
let computerChoice;
let result;
let userImg = document.createElement("img");
let computerImg = document.createElement("img");
userImg.className = "imgChoice";
computerImg.className = "imgChoice";
startGame.addEventListener("click", () => {
startGame.classList.add("hide");
containerGame.classList.remove("hide");
});
btns.forEach(function (btn) {
return btn.addEventListener("click", function (e) {
userChoice = e.target.id;
if (userChoice === "Rock") {
userImg.src = "./images/rock.png";
}
if (userChoice === "Paper") {
userImg.src = "./images/paper.png";
}
if (userChoice === "Scissors") {
userImg.src = "./images/scissors.png";
}
userChoiceDisplay.appendChild(userImg);
generateComputerChoice();
getResult();
getScore();
});
});
function generateComputerChoice() {
let randomNums = Math.ceil(Math.random() * 3);
if (randomNums === 1) {
computerChoice = "Rock";
computerImg.src = "./images/rock.png";
}
if (randomNums === 2) {
computerChoice = "Paper";
computerImg.src = "./images/paper.png";
}
if (randomNums === 3) {
computerChoice = "Scissors";
computerImg.src = "./images/scissors.png";
}
computerChoiceDisplay.appendChild(computerImg);
}
function getResult() {
if (userChoice === computerChoice) {
result = "It's a Draw!";
}
if (userChoice === "Rock" && computerChoice === "Paper") {
result = "You Lose!";
}
if (userChoice === "Rock" && computerChoice === "Scissors") {
result = "You Won!";
}
if (userChoice === "Paper" && computerChoice === "Rock") {
result = "You Won!";
}
if (userChoice === "Paper" && computerChoice === "Scissors") {
result = "You Lose!";
}
if (userChoice === "Scissors" && computerChoice === "Paper") {
result = "You Won!";
}
if (userChoice === "Scissors" && computerChoice === "Rock") {
result = "You Lose!";
}
resultDisplay.innerHTML = result;
}
let count = 0;
function getScore() {
if (result === "You Won!") {
count++;
}
if (result === "You Lose!") {
count--;
}
if (count <= 0) count = 0;
score.innerHTML = count;
}