-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
78 lines (71 loc) · 2.1 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const choices = document.querySelectorAll(".choices");
const msg = document.querySelector("#msg");
let uScore = document.querySelector("#user");
let cScore = document.querySelector("#comp");
let userScore = 0;
let compScore = 0;
const genComp = () => {
let choice = ["paper", "rock", "scissors"];
let compChoice = (Math.floor(Math.random() * 3));
return choice[compChoice];
}
const draw = () => {
console.log("Draw");
msg.innerText = "Game Was Draw.";
msg.style.backgroundColor = "#ffc300";
// msg.classList.add("draw");
}
const showWinner = (userWin,userChoice, compChoice) => {
if(userWin){
userScore++;
uScore.innerText = `${userScore}`;
console.log("Win");
msg.innerText = `Computer's Choice was ${compChoice}, You Win !`;
msg.style.backgroundColor = "#80b918";
// msg.classList.add("win");
}
else{
compScore++;
cScore.innerText = `${compScore}`;
console.log("Lose");
msg.innerText = `Computer's Choice was ${compChoice}, You Lose !`;
msg.style.backgroundColor = "#fc2f00";
// msg.classList.add("lose");
}
}
const playGame = (userChoice) => {
console.log("User's Choice : ",userChoice);
let compChoice = genComp();
console.log("Computer's Choice : ",compChoice);
if(userChoice === compChoice){
draw();
}
else{
let userWin = true;
if(userChoice === "paper"){
//rock scissors
if(compChoice === "scissors"){
userWin = false;
}
}
else if(userChoice === "rock"){
//paper scissors
if(compChoice === "paper"){
userWin = false
}
}
else{
if(compChoice === "rock"){
userWin = false;
}
}
showWinner(userWin, userChoice, compChoice);
}
}
choices.forEach((choice) => {
choice.addEventListener("click", () => {
let userChoice = choice.getAttribute("id");
// console.log("Choice was clicked was : ",userChoice);
playGame(userChoice);
});
});