-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (61 loc) · 2.02 KB
/
script.js
File metadata and controls
77 lines (61 loc) · 2.02 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
const choices = document.querySelectorAll(".box");
const genCompChoice = () => {
const options = ["rock", "paper", "scissor"];
const randIndx = Math.floor(Math.random() * 3);
return options[randIndx];
}
let msg = document.querySelector(".msg");
const showWinner = (userWin) =>{
if(userWin){
msg.innerText = "You are the Winner!"
}else
msg.innerText = "You Lost, Computer is the Winner!";
}
const userScore = document.querySelector(".score1");
const compScore = document.querySelector(".score2");
const countWinner = (userWin) => {
if(userWin){
userScore.textContent = Number(userScore.textContent) + 1;
}
else {
compScore.textContent = Number(compScore.textContent) + 1;
}
}
let userWin;
const playGame = (userChoice) => {
//now its time to generate the computer's choice
let compChoice = genCompChoice();//Modular way, that means
// for every single work there will be a seperate function,
// when ever we need to do that work , we can simply call the function.
console.log("Computer Choice:", compChoice);
if(userChoice === compChoice){
msg.innerText = "Tie";
}
else {
userWin = true;
if(userChoice === "rock"){
userWin = compChoice === "paper"? false: true;
}else if (userChoice ==="paper"){
userWin = compChoice === "rock"? true : false;
}else if (userChoice ==="scissor"){
userWin = compChoice === "rock"? false: true;
}
dupUserWin = userWin;
showWinner(userWin);
countWinner(userWin);
}
}
let btn = document.querySelector(".btn");
btn.addEventListener("click", (userWin) => {
userWin = true;
userScore.textContent = "0";
compScore.textContent = "0";
msg.innerText = "Let's PLay";
});
choices.forEach( (choice) => {
choice.addEventListener("click", ()=>{
let userChoice = choice.getAttribute("id");
console.log("User Choice:", userChoice);
playGame(userChoice);
})
})