-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (58 loc) · 1.74 KB
/
index.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
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
const computerChoiceDisplay = document.getElementById("computer-choice");
const playerChoiceDisplay = document.getElementById("player-choice");
const resultDisplay = document.getElementById("result");
const choices = document.querySelectorAll("button");
let playerChoice;
let computerChoice;
let result;
choices.forEach((choice) =>
choice.addEventListener("click", function (e) {
playerChoice = e.target.id;
playerChoiceDisplay.innerHTML = playerChoice;
generateComputerChoice();
getResult();
})
);
function generateComputerChoice() {
let randomNumber = Math.floor(Math.random() * choices.length) + 1;
if (randomNumber === 1) {
computerChoice = "Rock";
}
if (randomNumber === 2) {
computerChoice = "Paper";
}
if (randomNumber === 3) {
computerChoice = "Scissors";
}
computerChoiceDisplay.innerHTML = computerChoice;
}
function getResult() {
if (playerChoice === "Rock" && computerChoice === "Rock") {
result = "Draw!";
}
if (playerChoice === "Rock" && computerChoice === "Paper") {
result = "You Lost!";
}
if (playerChoice === "Rock" && computerChoice === "Scissors") {
result = "You Win!";
}
if (playerChoice === "Paper" && computerChoice === "Rock") {
result = "You Win!";
}
if (playerChoice === "paper" && computerChoice === "Paper") {
result = "Draw!";
}
if (playerChoice === "Paper" && computerChoice === "Scissors") {
result = "You Lost!";
}
if (playerChoice === "Scissors" && computerChoice === "Rock") {
result = "You Lost!";
}
if (playerChoice === "Scissors" && computerChoice === "Paper") {
result = "You Win!";
}
if (playerChoice === "Scissors" && computerChoice === "Scissors") {
result = "Draw!";
}
resultDisplay.innerHTML = result;
}