-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
77 lines (67 loc) · 2.26 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
let user_score=0;
let comp_score=0;
let user_scorepara=document.querySelector("#user-score");
let comp_scorepara=document.querySelector("#comp-score");
let msg=document.querySelector('#msg');
const choices=document.querySelectorAll(".choice");
choices.forEach((choice) =>{
choice.addEventListener("click",() =>{
let userChoice=choice.getAttribute('id');
playGame(userChoice)
})
})
let compChoice = ()=>{
let cho=['Rock','Paper','scissor'];
let indx=Math.floor(Math.random()*3);
return cho[indx];
}
let playGame = (userChoice)=>{
const comChoice=compChoice();
if(comChoice==userChoice){
console.log("draw the game");
msg.innerText ="draw the game";
msg.style.backgroundColor="grey";
}
else if(comChoice=="Rock" && userChoice=="Scissor"){
console.log("com is win");
msg.innerText ="com is win";
msg.style.backgroundColor="red";
comp_score++;
comp_scorepara.innerText=comp_score;
}
else if(comChoice=="Scissor" && userChoice=="Rock"){
console.log("you win!");
msg.innerText ="you win!";
msg.style.backgroundColor="green";
user_score++;
user_scorepara.innerText=user_score;
}
else if(comChoice=="Rock" && userChoice=="Paper"){
console.log("you win!");
msg.innerText ="you win!";
msg.style.backgroundColor="green";
user_score++;
user_scorepara.innerText=user_score;
}
else if(comChoice=="Paper" && userChoice=="Rock"){
console.log("comp win!");
msg.innerText ="comp win!";
msg.style.backgroundColor="red";
comp_score++;
comp_scorepara.innerText=comp_score;
}
else if(comChoice=="Paper" && userChoice=="Scissor"){
console.log("you win!");
msg.innerText ="you win!";
msg.style.backgroundColor="green";
user_score++;
user_scorepara.innerText=user_score;
}
else if(comChoice=="Scissor" && userChoice=="Paper"){
console.log("comp win!");
msg.innerText ="comp win!";
msg.style.backgroundColor="red";
comp_score++;
comp_scorepara.innerText=comp_score;
}
}