-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
app.js
78 lines (73 loc) · 2.64 KB
/
app.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
73
74
75
76
77
78
const resultText = document.getElementById("result");
const playerChoice = document.getElementById("player-choice");
const computerChoice = document.getElementById("computer-choice");
const playerMove = document.getElementById("playerMove");
const computerMove = document.getElementById("computerMove");
const rockBtn = document.getElementById("move1");
const paperBtn = document.getElementById("move2");
const scissorBtn = document.getElementById("move3");
let playerScore=0;
let computerScore=0;
rockBtn.addEventListener("click", ()=>{
playerChoice.src = "./images/1.png";
randomVal = Math.floor(Math.random()*3+1);
computerChoice.src = `./images/${randomVal}.png`;
playerMove.innerHTML = "Rock";
if(randomVal==1){ // R-R
resultText.innerHTML = "DRAW";
resultText.style.color = "blue";
computerMove.innerHTML = "Rock";
}
else if(randomVal==2){ // R-P
resultText.innerHTML = "You lose the game";
resultText.style.color = "red";
computerMove.innerHTML = "Paper";
}
else if(randomVal==3){ // R-S
resultText.innerHTML = "You win the game";
resultText.style.color = "green";
computerMove.innerHTML = "Scissor";
}
})
paperBtn.addEventListener("click", ()=>{
playerChoice.src = "./images/2.png";
randomVal = Math.floor(Math.random()*3+1);
computerChoice.src = `./images/${randomVal}.png`;
playerMove.innerHTML = "Paper";
if(randomVal==1){ // P-R
resultText.innerHTML = "You win the game";
resultText.style.color = "green";
computerMove.innerHTML = "Rock";
}
else if(randomVal==2){ // P-P
resultText.innerHTML = "DRAW";
resultText.style.color = "blue";
computerMove.innerHTML = "Paper";
}
else if(randomVal==3){ // P-S
resultText.innerHTML = "You lose the game";
resultText.style.color = "red";
computerMove.innerHTML = "Scissor";
}
})
scissorBtn.addEventListener("click", ()=>{
playerChoice.src = "./images/3.png";
randomVal = Math.floor(Math.random()*3+1);
computerChoice.src = `./images/${randomVal}.png`;
playerMove.innerHTML = "Scissor";
if(randomVal==1){ // S-R
resultText.innerHTML = "You lose the game";
resultText.style.color = "red";
computerMove.innerHTML = "Rock";
}
else if(randomVal==2){ // S-P
resultText.innerHTML = "You win the game";
resultText.style.color = "green";
computerMove.innerHTML = "Paper";
}
else if(randomVal==3){ // S-S
resultText.innerHTML = "DRAW";
resultText.style.color = "blue";
computerMove.innerHTML = "Scissor";
}
})