-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
47 lines (39 loc) · 1.42 KB
/
Copy pathscript.js
File metadata and controls
47 lines (39 loc) · 1.42 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
let humanScore = 0;
let computerScore = 0;
const messageDiv = document.getElementById("message");
const scoreDiv = document.getElementById("score");
const buttons = document.querySelectorAll("button");
function getComputerChoice() {
const choices = ["rock", "paper", "scissors"];
return choices[Math.floor(Math.random() * choices.length)];
}
function playRound(humanChoice) {
if (humanScore === 5 || computerScore === 5) return;
const computerChoice = getComputerChoice();
if (humanChoice === computerChoice) {
messageDiv.textContent = `Tie! Both chose ${humanChoice}`;
} else if (
(humanChoice === "rock" && computerChoice === "scissors") ||
(humanChoice === "paper" && computerChoice === "rock") ||
(humanChoice === "scissors" && computerChoice === "paper")
) {
humanScore++;
messageDiv.textContent = `You win! ${humanChoice} beats ${computerChoice}`;
} else {
computerScore++;
messageDiv.textContent = `You lose! ${computerChoice} beats ${humanChoice}`;
}
scoreDiv.textContent = `You: ${humanScore} | Computer: ${computerScore}`;
if (humanScore === 5) {
messageDiv.textContent = "🎉 You won the game!";
} else if (computerScore === 5) {
messageDiv.textContent = "💀 Computer won the game!";
}
}
// event listeners
buttons.forEach(button => {
button.addEventListener("click", () => {
const humanChoice = button.dataset.choice;
playRound(humanChoice);
});
});