-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (52 loc) · 1.6 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
userScore = 0;
compScore = 0;
const choices = document.querySelectorAll(".imgContainer");
const msg = document.querySelector("#msg");
const userS = document.querySelector("#user-Score");
const compS = document.querySelector("#comp-Score");
const genCompChoice = () => {
const option = ["rock", "paper", "scissors"];
const randIdx = Math.floor(Math.random() * 3);
return option[randIdx];
}
const draw = () => {
msg.innerText = "Draw!";
msg.style.backgroundColor = "#181818";
}
const showWinner = (userWin) => {
if (userWin) { // Changed to use boolean directly
userScore++;
userS.innerText = userScore;
msg.innerText = "You Win!";
msg.style.backgroundColor = "green";
} else {
compScore++;
compS.innerText = compScore;
msg.innerText = "You lose!";
msg.style.backgroundColor = "red";
}
}
const gamePlay = (userChoice) => {
const compChoice = genCompChoice();
console.log(compChoice);
if (userChoice === compChoice) {
draw();
} else {
let userWin = true;
if (userChoice === "rock") {
userWin = compChoice === "paper" ? false : true;
} else if (userChoice === "paper") {
userWin = compChoice === "scissors" ? false : true;
} else {
userWin = compChoice === "rock" ? false : true;
}
showWinner(userWin);
}
}
choices.forEach((imgContainer) => {
imgContainer.addEventListener("click", () => {
const userChoice = imgContainer.getAttribute("id");
console.log(userChoice);
gamePlay(userChoice);
})
})