-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
36 lines (27 loc) · 1.14 KB
/
game.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
const choices = document.querySelectorAll(".choice");
const resultText = document.getElementById("msg");
const userChoiceText = document.getElementById("user-choice");
const compChoiceText = document.getElementById("comp-choice");
const c = ["tiger", "tie", "dragon"];
choices.forEach((choice) => {
choice.addEventListener("click", () => {
const userChoice =choice.getAttribute("id");
const compChoice = c[Math.floor(Math.random() * 3)];
userChoiceText.innerText = userChoice;
compChoiceText.innerText = compChoice;
if (userChoice === compChoice) {
resultText.innerText = "you won,congratulations!";
resultText.style.backgroundColor="rgb(0, 255, 20)";
}
else {
if(compChoice=="tie")
{
resultText.innerText = "draw ! play again...";
resultText.style.backgroundColor="rgb(255, 255, 0)";
}
else{
resultText.innerText = "you lose! Better luck next time" ;
resultText.style.backgroundColor="red";}
}
});
});