-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
88 lines (78 loc) · 2.35 KB
/
Copy pathscript.js
File metadata and controls
88 lines (78 loc) · 2.35 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
let userScore=0;
let compScore=0;
const choices=document.querySelectorAll('.choice');
const msg=document.querySelector("#msg");
const uScore = document.querySelector("#userScore");
const cScore = document.querySelector("#compScore");
const genCompChoice = () =>{
//generation of computer choice
const options=["rock","paper","sissors"];
const randomIdx = Math.floor(Math.random() * 3);
return options[randomIdx];
};
let showWinner = (userWin,userChoice,compChoice) =>{
if(userWin){
userScore++;
uScore.innerText = userScore;
console.log("You Win!");
msg.innerText=`You Win! ${userChoice} beats ${compChoice}`;
msg.style.backgroundColor="green";
}else{
compScore++;
cScore.innerText = compScore;
console.log("You lose!");
msg.innerText=`You Lose! ${compChoice} beats ${userChoice}`;
msg.style.backgroundColor="red";
}
};
const playGame = (userChoice) =>{
console.log("User choice =",userChoice);
const compChoice=genCompChoice();
console.log("Comp choice =",compChoice);
if(userChoice==compChoice){
//Draw game
drawGame();
} else{
let userWin= true;
if(userChoice === "rock"){
//sissors,paper
userWin = compChoice==="paper"? false:true ;
}
else if(userChoice === "paper"){
//rock,sissors
userWin = compChoice==="sissors"? false : true;
}
else{
//rock,paper
userWin = compChoice==="rock"? false : true;
}
showWinner(userWin,userChoice,compChoice);
}
};
const drawGame =()=>{
console.log("Game was Draw!");
msg.innerText="It is a draw!Play Again!";
msg.style.backgroundColor="#6934cb";
};
choices.forEach((choice)=>{
choice.addEventListener("click",()=>{
const userChoice = choice.getAttribute("id");
playGame(userChoice);
})
});
//Change mode button
let modeBtn=document.querySelector("#mode");
let currMode="light";
let body=document.querySelector("body");
modeBtn.addEventListener("click",()=>{
if(currMode==="light"){
currMode="dark";
body.classList.add("dark");
body.classList.remove("light");
} else{
currMode="light";
body.classList.add("light");
body.classList.remove("dark")
}
console.log(currMode);
})