-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
98 lines (86 loc) · 2.51 KB
/
javascript.js
File metadata and controls
98 lines (86 loc) · 2.51 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
89
90
91
92
93
94
95
96
97
const images = Array.from(document.querySelectorAll('.Images'));
const message = document.querySelector(".message");
const playerScore = document.querySelector(".player-score");
const computerScore = document.querySelector(".computer-score");
const mforplayer = document.querySelector(".pdecision")
const mforcomputer = document.querySelector(".cdecision")
const image1 = document.querySelector(".img1");
var computerCount = 0;
var playerCount = 0;
images.forEach((img) =>
img.addEventListener('click',() => {
if(playerCount >=5 || computerCount >=5){
playerCount = 0;
computerCount = 0;
playerScore.textContent = "0";
computerScore.textContent = "0";
return;
}
else{
game(img.dataset.set)
}
}));
function computerChoice(){
let num = Math.floor(Math.random() * 3);
if(num===0){
return "rock"
}
if(num===1){
return "paper"
}
if(num===2){
return "scissor"
}
}
function checkingWhoWins(computerChoice, playerChoice){
let lPlayerChoice = playerChoice.toLowerCase();
if(lPlayerChoice === computerChoice){
return "It's a tie";
}
else if(lPlayerChoice === "rock"){
if(computerChoice === "paper"){
return "Computer Wins Round!!!"
}
else{
return "Player Wins Round!!!"
}
}
else if(lPlayerChoice === "paper"){
if(computerChoice === "scissor"){
return "Computer Wins Round!!!"
}
else{
return "Player Wins Round!!!"
}
}
else if(lPlayerChoice === "scissor"){
if(computerChoice === "rock"){
return "Computer Wins Round!!!"
}
else{
return "Player Wins Round!!!"
}
}
}
function game(playerchoice){
let pchoice = playerchoice.toLowerCase();
let cchoice = computerChoice();
let result = checkingWhoWins(cchoice,pchoice);
if(result === "Player Wins Round!!!"){
playerCount++;
}
else if(result === "Computer Wins Round!!!"){
computerCount++;
}
playerScore.textContent = playerCount;
computerScore.textContent = computerCount;
mforcomputer.textContent = `Player choose ${pchoice}`;
mforplayer.textContent = `Computer choose ${cchoice}`;
message.textContent = result;
if(playerCount === 5){
message.textContent = "Player Wins";
}
else if(computerCount === 5 ){
message.textContent = "Computer Wins";
}
}