-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
72 lines (63 loc) · 1.92 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
let userScore = 0;
let compScore= 0;
let choices = document.querySelectorAll(".choice");
let msg = document.querySelector("#msg");
//Access to score-board (score)
let userScorepara = document.querySelector("#user-score");
let compScorepara = document.querySelector("#comp-score");
//computer choice
const gencompChoice = () => {
const options = ["rock", "paper", "scissor"];
let randIdx= Math.floor(Math.random() * 3);
return options [randIdx];
}
//Draw game command ----
const drawGame = () =>{
console.log("game was draw.")
msg.innerText = "Game was draw play Again";
msg.style.backgroundColor = "yellow";
};
//ShowWinner function
let showWin = (userwin , Userchoice , compChoice) =>{
if (userwin){
userScore++;
userScorepara.innerText = userScore;
msg.innerText = "You win!";
msg.style.backgroundColor = "green";
} else{
compScore++;
compScorepara.innerText = compScore;
msg.innerText = "you lose";
msg.style.backgroundColor = "red";
}
}
//PlayGame Function
const playGame = (userChoice) => {
console.log("userChoice =", userChoice);
//generate computer choice
const compChoice = gencompChoice();
console.log("compChoice =", compChoice);
//Condition Check B/W User and computer ----
if(userChoice === compChoice){
drawGame();
} else {
let userwin = true;
if (userChoice === "Rock"){
//Scissors // paper
userwin = compChoice === "Paper" ? flase: true;
} else if (userChoice === "paper"){
userwin = compChoice === "Scissor" ? flase: true;
} else {
//rock //paper
userwin = compChoice === "rock"? false: true;
}
showWin (userwin);
} };
//Function of Userchoice
choices.forEach((choice)=>{
choice.addEventListener("click", ()=>{
const userChoice= choice.getAttribute("id");
// console.log("choice was clicked", userChoice);
playGame (userChoice);
})
});