-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (41 loc) · 1.82 KB
/
app.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
let options = document.querySelectorAll(".options");
let userScore = document.querySelector("#user-score");
let compScore = document.querySelector("#comp-score");
let messageContent = document.querySelector("#message-content");
const availableOptions = ["stone", "paper", "scissor"];
const userWonMsg = "Congratulations! You have won.";
const userLostMsg = "Sorry, you have lost! Try again.";
const drawMsg = "It is a Draw! Play again.";
const checkWinner = (evt) => {
const userInput = evt.target.id;
const compInput = availableOptions[Math.floor(Math.random() * 3)];
if (userInput === compInput) draw();
else {
// if(userInput === "stone" && compInput === "scissor") userWon();
// if(userInput === "stone" && compInput === "paper") userLost();
// if(userInput === "paper" && compInput === "stone") userWon();
// if(userInput === "paper" && compInput === "scissor") userLost();
// if(userInput === "scissor" && compInput === "paper") userWon();
// if(userInput === "scissor" && compInput === "stone") userLost();
userInput === "stone" && (compInput === "scissor" ? userWon() : userLost());
userInput === "paper" && (compInput === "stone" ? userWon() : userLost());
userInput === "scissor" && (compInput === "paper" ? userWon() : userLost());
}
};
const userWon = () => {
userScore.innerText = ++userScore.innerText;
messageContent.innerText = userWonMsg;
messageContent.style.backgroundColor = "rgb(170, 246, 131)";
};
const userLost = () => {
compScore.innerText = ++compScore.innerText;
messageContent.innerText = userLostMsg;
messageContent.style.backgroundColor = "rgb(247, 160, 114)";
};
const draw = () => {
messageContent.innerText = drawMsg;
messageContent.style.backgroundColor = "rgb(217, 229, 214)";
};
for (option of options) {
option.addEventListener("click", checkWinner);
}