-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
144 lines (112 loc) · 3.74 KB
/
Copy pathapp.js
File metadata and controls
144 lines (112 loc) · 3.74 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const computerChoiceDisplay = document.getElementById("computer-choice");
const userChoiceDisplay = document.getElementById("user-choice");
const resultDisplay = document.getElementById("result");
const main = document.querySelector("main");
const nav = document.querySelector("nav");
const winner = document.querySelector(".winner");
const startGame = document.querySelector(".opening-message");
let userChoice;
let result;
const score = {
user: 0,
comp: 0,
};
const getChoice = document.querySelectorAll(".btn")
getChoice.forEach(choice => choice.addEventListener("click", (e)=>{
userChoice = e.target.id;
// let element = document.getElementById(userChoice);
// let text = element.textContent;
userChoiceDisplay.innerHTML = userChoice;
if (userChoice === 'Rock'){
userChoiceDisplay.innerHTML = '<img src="img/Rock.png" alt="rock" width="100px">';
}
else if (userChoice === 'Paper'){
userChoiceDisplay.innerHTML = '<img src="img/Paper.png" alt="paper" width="100px">';
}
else if (userChoice === 'Scissors'){
userChoiceDisplay.innerHTML = '<img src="img/Scissors.png" alt="scissors" width="100px">';
}
console.log(userChoice);
generateComputerChoice();
getResult();
checkForGameEnd()
}))
function generateComputerChoice(){
const randomNumber = Math.floor(Math.random() * 3) + 1;
console.log(randomNumber);
if(randomNumber === 1){
computerChoice = 'Rock';
}
if(randomNumber === 2){
computerChoice = 'Scissors';
}
if(randomNumber === 3){
computerChoice = 'Paper';
}
computerChoiceDisplay.innerHTML = `<img src="img/${computerChoice}.png" alt="scissors" width="100px">`
// computerChoiceDisplay.innerHTML = computerChoice;
}
function getResult(){
if(computerChoice === userChoice){
result = `its a draw`
}
if(computerChoice === 'Rock' && userChoice === 'Paper'){
result = 'You win!';
score.user++;
}
if(computerChoice === 'Rock' && userChoice === 'Scissors'){
result = 'you lost!';
score.comp++;
}
if(computerChoice === 'Paper' && userChoice === 'Scissors'){
result = 'you win!';
score.user++;
}
if(computerChoice === 'Paper' && userChoice === 'Rock'){
result = 'you lost!';
score.comp++;
}
if(computerChoice === 'Scissors' && userChoice === 'Rock'){
result = 'you win!';
score.user++;
}
if(computerChoice === 'Scissors' && userChoice === 'Paper'){
result = 'you lost!';
score.comp++;
}
resultDisplay.innerHTML = result;
document.getElementById("user").innerHTML = `${score.user}`
document.getElementById("comp").innerHTML = `${score.comp}`
}
// Check Game end
function checkForGameEnd(){
const winnerDisplay = document.querySelector(".winner-message")
if(score.user === 10){
winner.style.display = "flex";
winnerDisplay.innerHTML = "🎊🎉 you WON the Game"
}
else if (score.comp === 10){
winner.style.display = "flex";
winnerDisplay.innerHTML = "😰 OPPS You LOST!"
}
}
const reset= document.querySelector(".restart")
reset.addEventListener("click", ()=>{
console.log("Reset button clicked");
main.style.display = "flex";
nav.style.display = "flex";
winner.style.display = "none";
user.innerHTML = 0;
comp.innerHTML = 0;
score.user = 0;
score.comp = 0;
resultDisplay.innerHTML = "RESULT";
userChoiceDisplay.innerHTML = "";
computerChoiceDisplay.innerHTML = "";
})
const start = document.querySelector(".btn-next")
start.addEventListener("click", ()=>{
main.style.display = "flex";
nav.style.display = "flex";
startGame.style.display = "none";
})