-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (59 loc) · 1.74 KB
/
Copy pathscript.js
File metadata and controls
66 lines (59 loc) · 1.74 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
62
63
64
65
66
let userscore = 0;
let compscore = 0;
const choices = document.querySelectorAll(".choice");
const msg = document.querySelector("#msg");
const userscorepara = document.querySelector("#user-score");
const compscorepara = document.querySelector("#computer-score");
const gencompchoice = () => {
const options =["rock","paper","scissors"];
const randomind = Math.floor(Math.random()*3);
return options[randomind];
}
const drawgame= () => {
console.log("Game drawn!!");
msg.innerText = "Game drawn!";
msg.style.backgroundColor = "#081b31";
}
const showwinner = (userwin) => {
if(userwin){
userscore++;
userscorepara.innerText = userscore;
console.log("You win!");
msg.innerText = "You Win!!";
msg.style.backgroundColor = "green";
}
else{
compscore++;
compscorepara.innerText = compscore;
console.log("You lose");
msg.innerText = "You lose :("
msg.style.backgroundColor = "red";
}
}
const playgame = (userchoice) =>{
console.log("user choice = ",userchoice);
const compchoice = gencompchoice();
console.log("computer choice = ",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);
}
}
choices.forEach((choice) => {
choice.addEventListener("click", () => {
const userchoice = choice.getAttribute("id");
playgame(userchoice);
});
});