-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
61 lines (60 loc) · 1.66 KB
/
script.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
let userScore=0;
let compScore=0;
const UserScorePara=document.querySelector("#you");
const CompScorePara=document.querySelector("#computer");
const choices = document.querySelectorAll(".choice");
const msg=document.querySelector("#msg");
const showWinner=(userWin)=>{
if(userWin){
userScore++;
UserScorePara.innerText=userScore;
console.log("YOU WON!");
msg.innerText="You Won!";
msg.style.backgroundColor="green";
}
else{
compScore++;
CompScorePara.innerText=compScore;
console.log("COMPUTER WIN!");
msg.innerText="Computer Win";
msg.style.backgroundColor="red";
}
}
const draw=()=>{
console.log("Game Was Draw");
msg.innerText="Draw Play Again!";
msg.style.backgroundColor="#081b31";
}
const GenerateComp=()=>{
const options=["rock","paper","scissor"];
const randomIdx=Math.floor(Math.random()*3);
return options[randomIdx];
}
const PlayGame=(userChoice)=>{
console.log("User Choice is :",userChoice);
const comChoice=GenerateComp();
console.log("Computer Choice is :",comChoice);
if(userChoice===comChoice){
draw();
}
else{
let userWin=true;
if(userChoice==="rock"){
userWin=comChoice==="paper"?false:true;
}
else if(userChoice==="paper"){
userWin=comChoice==="scissor"?false:true;
}
else{
useWin=comChoice=="rock"?false:true;
}
showWinner(userWin);
}
}
choices.forEach((choice)=>{
console.log(choice);
choice.addEventListener("click",()=>{
const userChoice=choice.getAttribute("id")
PlayGame(userChoice);
})
})