-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (54 loc) · 1.69 KB
/
script.js
File metadata and controls
61 lines (54 loc) · 1.69 KB
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 choice = document.querySelectorAll(".choice");
const userScorePara = document.querySelector("#your-score");
const compScorePara = document.querySelector("#comp-score");
let msg = document.querySelector("#msg");
const genCompChoice = () => {
const options = ["rock" , "paper" , "scissors"];
const randIdx = Math.floor(Math.random() * 3);
return options[randIdx];
}
const drawGame= () => {
msg.innerText = "Game Was Drawn!";
msg.style.backgroundColor = "Grey"
}
const showWinner = (userWin , userchoice , compchoice) =>{
if(userWin){
userScore++;
userScorePara.innerText = userScore;
msg.innerText = `You Win! Your ${userchoice} beats ${compchoice}`;
msg.style.backgroundColor = "green";
}
else{
compScore++;
compScorePara.innerText = compScore;
msg.innerText = `You did not Win. ${compchoice} beats ${userchoice}`;
msg.style.backgroundColor = "Red";
}
}
const playGame = (userchoice) => {
const compchoice = genCompChoice ();
if (userchoice === compchoice) {
drawGame();
}
else {
let userWin = true;
if (userchoice === "rock") {
userWin = compchoice === "paper" ? false : true;
}
else if (userchoice === "scissors") {
userWin = compchoice === "rock" ? false : true;
}
else {
userWin = compchoice === "scissors" ? false : true;
}
showWinner(userWin , userchoice , compchoice);
}
}
choice.forEach((choice) => {
choice.addEventListener("click" , () => {
const userchoice = choice.getAttribute("id");
playGame(userchoice);
})
});