-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
39 lines (34 loc) · 1.09 KB
/
Copy pathscript.js
File metadata and controls
39 lines (34 loc) · 1.09 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
const choice = document.querySelectorAll(".choice");
const playerScore = document.getElementById("user-score");
const computerScore = document.getElementById("comp-score");
const result = document.getElementById("result-message");
let playerPoints = 0;
let computerPoints = 0;
choice.forEach((btn) => {
btn.addEventListener('click', () => {
winner(btn.id);
})
})
const choices = ["rock", "paper", "scissors"];
// Function to get computer's choice
const computerChoice = () => {
const random = Math.floor(Math.random() * choices.length);
return choices[random];
}
// Function to determine the winner
const winner = (player) => {
const comp = computerChoice();
if(comp === "rock" && player === "scissors" ||
comp === "paper" && player === "rock" ||
comp === "scissors" && player === "paper") {
computerPoints++;
computerScore.innerText = computerPoints;
result.innerText = "Computer wins!";
} else if(comp === player) {
result.innerText = "It's a tie!";
} else {
playerPoints++;
playerScore.innerText = playerPoints;
result.innerText = "You win!";
}
}