-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
66 lines (61 loc) · 1.87 KB
/
script.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
let user = document.querySelector("#user-score");
let comp = document.querySelector("#comp-score");
let msg = document.querySelector("#msg");
let userScore = 0;
let compScore = 0;
const choice = document.querySelectorAll(".ch");
const genCompChoice = () => {
let options = ["rock", "paper", "scissors"];
const randomindx = Math.floor(Math.random() * 3);
return options[randomindx];
}
const playGame = (userChoice) => {
console.log(`User Choice = ${userChoice}`);
const compChoice = genCompChoice();
console.log(`Computer Choice = ${compChoice}`);
if (userChoice === compChoice) {
return msg.innerText = "TIE";
}
if (compChoice === "rock") {
if (userChoice == "scissors") {
compScore++;
comp.innerText = compScore;
return msg.innerText = "Computer Score";
}
else {
userScore++;
user.innerText = userScore;
return msg.innerText = "User Score";
}
}
if (compChoice === "paper") {
if (userChoice == "rock") {
compScore++;
comp.innerText = compScore;
return msg.innerText = "Computer Score";
}
else {
userScore++;
user.innerText = userScore;
return msg.innerText = "User Score";
}
}
if (compChoice === "scissors") {
if (userChoice == "paper") {
compScore++;
comp.innerText = compScore;
return msg.innerText = "Computer Score";
}
else {
userScore++;
user.innerText = userScore;
return msg.innerText = "User Score";
}
}
}
choice.forEach((ch) => {
ch.addEventListener("click", () => {
const userChoice = ch.getAttribute("id")
playGame(userChoice);
})
})