-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
40 lines (34 loc) · 1.54 KB
/
script.js
File metadata and controls
40 lines (34 loc) · 1.54 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
const Rock = document.getElementById("Rock");
const Paper = document.getElementById("Paper");
const Scissor = document.getElementById("Scissor");
const PlayerChoice = document.getElementById("Player-choice");
const ComputerChoice = document.getElementById("Computer-choice");
const Result = document.getElementById("Result");
const PlayerScore = document.getElementById("Player-Score");
const ComputerScore = document.getElementById("Computer-score");
let playerScore = 0;
let computerScore = 0;
function Game(playerSelection) {
const choices = ["Rock", "Paper", "Scissor"];
const computerSelection = choices[Math.floor(Math.random() * 3)];
PlayerChoice.textContent = `Player-choice: ${playerSelection}`;
ComputerChoice.textContent = `Computer-choice: ${computerSelection}`;
if (playerSelection === computerSelection) {
Result.textContent = "It's a Tie!";
} else if (
(playerSelection === "Rock" && computerSelection === "Scissor") ||
(playerSelection === "Paper" && computerSelection === "Rock") ||
(playerSelection === "Scissor" && computerSelection === "Paper")
) {
Result.textContent = "You Win!";
playerScore++;
} else {
Result.textContent = "Computer Wins!";
computerScore++;
}
PlayerScore.textContent = `Player: ${playerScore}`;
ComputerScore.textContent = `Computer: ${computerScore}`;
}
Rock.addEventListener("click", () => Game("Rock"));
Paper.addEventListener("click", () => Game("Paper"));
Scissor.addEventListener("click", () => Game("Scissor"));