-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (64 loc) · 1.74 KB
/
index.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
const container = document.querySelector(".game");
const result = document.querySelector(".result");
function getCompChoice() {
let choice = Math.floor(Math.random() * 3) + 1
if(choice == 1) {
return "rock"
}else if(choice == 2){
return "scissor"
}
return "paper"
}
function playRound(pc, cc) {
let ans;
if(pc == "rock" && cc == "rock") {
ans = "rock";
}else if(pc == "paper" && cc == "paper") {
ans = "paper";
}
else if(pc == "scissor" && cc == "scissor") {
console.log("scissor");
return;
}
if(pc == "rock" && cc == "scissor"){
console.log("won rock")
}else if(pc == "paper" && cc == "rock"){
console.log("won paper")
}else if(pc == "scissor" && cc == "paper") {
console.log("won scissor");
}else if(pc == "scissor" && cc == "rock") {
console.log("computer wins")
}else if(pc == "paper" && cc == "scissor"){
console.log("comp wins")
}else if(pc == "rock" && cc == "paper") {
console.log("comp wins");
}
else {
console.log("default condition");
}
result.textContent = ans;
}
const choosedPlay = (e) => {
return e.target.textContent;
}
//This is the actual game div
let counter = 1;
// buttons
// let playerChoice;
const plays = [...document.querySelectorAll(".btn-pl")];
plays.forEach(btn => {
btn.addEventListener('click', (e) => {
playGame(e);
})
});
// console.log(playerChoice);
const playGame = (e) => {
if(counter < 5){
let playerChoice = choosedPlay(e).toLowerCase();
let compChoice = getCompChoice();
playRound(playerChoice, compChoice)
counter++;
}else {
result.textContent = "You Lose Or Won";
}
}