-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
62 lines (54 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
52
53
54
55
56
57
58
59
60
61
let userScore = 0;
let compScore = 0;
const choices = document.querySelectorAll(".choice")
let msg = document.querySelector("#msg")
let userScorePara = document.querySelector("#user-score")
let compScorePara = document.querySelector("#comp-score")
const getCompChoice = () => {
const options = ["rock", "paper", "scissors"]
const randIndex = Math.floor(Math.random() * 3);
return options[randIndex];
}
const drawGame = () => {
msg.innerText = "It's a Draw. Play Again";
msg.style.backgroundColor = "#081b31";
}
const showWinner = (userWin, userChoice, compChoice) => {
if (userWin) {
userScore = userScore + 1
userScorePara.innerText = `${userScore}`;
msg.innerText = `You won! Your ${userChoice} beats ${compChoice}.`;
msg.style.backgroundColor = "rgb(36, 215, 36)"
}
else {
compScore = compScore + 1
compScorePara.innerText = `${compScore}`;
msg.innerText = `You lost. ${compChoice} beats your ${userChoice}.`;
msg.style.backgroundColor = "rgb(232, 29, 29)";
}
}
const playGame = (userChoice) => {
console.log("User choice is ", userChoice);
const compChoice = getCompChoice();
console.log("Comp choice is ", compChoice);
if (userChoice === compChoice) {
drawGame()
}
else {
let userWin = true;
if (userChoice === "rock") {
userWin = compChoice === "paper" ? false : true;
}
else if (userChoice === "paper") {
userWin = compChoice === "rock" ? true : false
}
else { userWin = compChoice === "paper" ? true : false }
showWinner(userWin, userChoice, compChoice);
}
}
choices.forEach((choice) => {
choice.addEventListener("click", () => {
const userChoice = choice.getAttribute("id");
playGame(userChoice);
})
})