Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (53 loc) · 1.7 KB
/
Copy pathscript.js
File metadata and controls
61 lines (53 loc) · 1.7 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 computerScore = 0 ;
const choices = document.querySelectorAll('.choice');
const mes = document.querySelector('#msg');
const userScoreP = document.querySelector('#user-score');
const CompScoreP = document.querySelector('#computer-score');
const genComputerchoice = ()=>{
const option = ["rock","paper","scissors"];
const num = Math.floor(Math.random()*3);
return option[num];
}
const drawGame = ()=>{
console.log("Game was Draw !");
mes.innerText = "Game Was Draw";
mes.style.backgroundColor = "#081b31"
}
const ShowWinner=(Userwin)=>{
if(Userwin){
userScore++;
userScoreP.innerText = userScore;
mes.innerText = "You Win";
mes.style.backgroundColor = "green";
}else{
computerScore++;
CompScoreP.innerText = computerScore;
mes.innerText = "Oops, You Loss Play Again!";
mes.style.backgroundColor = "red";
}
}
const playGame = (userChoice)=>{
console.log("user Choice is = ",userChoice);
//Generate computer choice -> Modular
const compChoice = genComputerchoice();
if(userChoice===compChoice){
drawGame();
}else{
let Userwin = true;
if(userChoice==="rock"){
Userwin = compChoice==="paper" ? false :true;
}else if (userChoice ==="paper"){
Userwin = compChoice==="scissors" ? false :true;
}else{
Userwin = compChoice==="rock" ? false :true;
}
ShowWinner(Userwin);
}
}
choices.forEach((choice)=>{
choice.addEventListener('click',()=>{
const userChoice = choice.getAttribute('id');
playGame(userChoice);
})
})