-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
110 lines (103 loc) · 3.47 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
let userscore = 0;
let compscore = 0;
let pgt = document.querySelectorAll(".choice ");
let msg=document.querySelector("#message");
let count1=document.querySelector("#count1");
let count2=document.querySelector("#count2");
restart=document.querySelector("#restart");
// Function to get computer's choice
const computerChoice = () => {
const variables = ['rock', 'paper', 'scissors'];
const random = Math.floor(Math.random() * 3);
return variables[random];
};
playgame=((userchoice)=>{
comChoice=computerChoice();
if(userchoice===comChoice){
console.log("draw");
msg.innerText="draw";
msg.style.backgroundColor="yellow";
msg.style.color="black";
}
else if(userchoice=="rock"){
if (comChoice=="scissors") {
console.log(`You Win! your ${userchoice} beats ${comChoice}`);
msg.innerText=`You Win! your ${userchoice} beats ${comChoice}`;
msg.style.backgroundColor="green";
userscore++;
}
if (comChoice=="paper") {
console.log(`You loose! ${comChoice} beats your ${userchoice}`);
msg.innerText=`You loose! ${comChoice} beats your ${userchoice}`;
msg.style.backgroundColor="red";
compscore++;
}
}
else if(userchoice=="paper"){
if (comChoice=="rock") {
console.log(`You Win! your ${userchoice} beats ${comChoice}`);
msg.innerText=`You Win! your ${userchoice} beats ${comChoice}`;
msg.style.backgroundColor="green";
userscore++;
}
if (comChoice=="scissors") {
console.log(`You loose! ${comChoice} beats your ${userchoice}`);
msg.innerText=`You loose! ${comChoice} beats your ${userchoice}`;
msg.style.backgroundColor="red";
compscore++;
}
}
else if(userchoice=="scissors"){
if (comChoice=="paper") {
console.log(`You Win! your ${userchoice} beats ${comChoice}`);
msg.innerText=`You Win! your ${userchoice} beats ${comChoice}`;
msg.style.backgroundColor="green";
userscore++;
}
if (comChoice=="rock") {
console.log(`You loose! ${comChoice} beats your ${userchoice}`);
msg.innerText=`You loose! ${comChoice} beats your ${userchoice}`;
msg.style.backgroundColor="red";
compscore++;
}
}
count1.innerText=userscore;
count2.innerText=compscore;
finalresult();
})
const disableButtons = () => {
pgt.forEach(button => {
button.disabled = true;
});
};
finalresult=()=>{
if (userscore>=5) {
msg.innerText="You Win! Congratulations";
msg.style.backgroundColor="green";
}
else if (compscore>=5) {
msg.innerText="You lose! Better luck next time";
msg.style.backgroundColor="red";
}
}
const reset=()=>{
count1.innerText=0;
count2.innerText=0;
userscore=0;
compscore=0;
pgt.forEach(choose => {
choose.disabled = false;
});
msg.innerText="pick your container";
msg.style.backgroundColor="yellow";
msg.style.color="black";
}
restart.addEventListener('click',()=>{
reset();
})
pgt.forEach((choose) => {
choose.addEventListener('click', () => {
const userchoice = choose.getAttribute("id");
playgame(userchoice);
});
});