-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
43 lines (37 loc) · 1.1 KB
/
script.js
File metadata and controls
43 lines (37 loc) · 1.1 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
const user = document.getElementById("userchoice");
const computer = document.getElementById("computerchoice");
const resultd = document.getElementById("result");
const output = document.querySelectorAll("button");
let userchoice;
let computerchoice;
let result;
output.forEach((button) => {
button.addEventListener("click", (e) => {
userchoice = e.target.id.toLowerCase();
user.innerHTML = userchoice;
randomc();
});
});
function randomc() {
const rn = Math.floor(Math.random() * 3) + 1;
if (rn === 1) {
computerchoice = "rock";
} else if (rn === 2) {
computerchoice = "scissors";
} else {
computerchoice = "paper";
}
computer.innerHTML = computerchoice;
if (userchoice === computerchoice) {
result = "It's a tie!";
} else if (
(userchoice === "rock" && computerchoice === "scissors") ||
(userchoice === "paper" && computerchoice === "rock") ||
(userchoice === "scissors" && computerchoice === "paper")
) {
result = "You win!";
} else {
result = "Computer wins!";
}
resultd.innerHTML = result;
}