-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
41 lines (36 loc) · 1.23 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
let computerChoice = ["rock", "paper", "scissors"];
function getComputerChoice() {
return computerChoice[Math.floor(Math.random() * computerChoice.length)];
}
let playerScore = 0;
let computerScore = 0;
function playRound(playerSelection, computerSelection) {
if (computerSelection === playerSelection) {
// It's a tie
} else if (
(computerSelection === "rock" && playerSelection === "paper") ||
(computerSelection === "paper" && playerSelection === "scissors") ||
(computerSelection === "scissors" && playerSelection === "rock")
) {
console.log("player won");
playerScore++;
document.getElementById("player").textContent = playerScore;
} else {
console.log("player lost");
computerScore++;
document.getElementById("computer").textContent = computerScore;
}
}
//-----------DOM----------------//
let playerSelection = "";
const btn = document.querySelectorAll(".btn");
const getSelected = (e) => {
playerSelection = e.target.id;
const computerSelection = getComputerChoice();
console.log("Player:", playerSelection);
console.log("Computer:", computerSelection);
playRound(playerSelection, computerSelection);
};
for (const button of btn) {
button.addEventListener("click", getSelected);
}