-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
63 lines (56 loc) · 1.99 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
let userScore = 0;
let compScore = 0;
let choices = document.querySelectorAll(".choice");
let message = document.querySelector("#msg");
let userScoreDisplay = document.querySelector("#userscore");
let compScoreDisplay = document.querySelector("#compscore");
const genCompChoice = () => {
const options = ["rock", "paper", "scissors"];
const randIndex = Math.floor(Math.random() * 3);
return options[randIndex];
};
const drawgame = () => {
console.log("Game is draw");
message.innerText = "The game is a draw, play again";
message.style.backgroundColor = "black";
};
const showwinner = (userwin, userChoice, compChoice) => {
if (userwin) {
console.log("You won");
message.innerText = `You win! Lucky head. Your ${userChoice} beats ${compChoice}`;
message.style.backgroundColor = "green";
userScore++;
userScoreDisplay.innerText = userScore;
} else {
console.log("You lost");
message.innerText = `You lost, Noob. ${compChoice} beats your ${userChoice}`;
message.style.backgroundColor = "red";
compScore++;
compScoreDisplay.innerText = compScore;
}
};
const playgame = (choiceId) => {
console.log("UserChoice was =", choiceId);
const compChoice = genCompChoice();
console.log("CompChoice =", compChoice);
if (choiceId === compChoice) {
drawgame();
} else {
let userwin = true;
if (choiceId === "rock") {
userwin = compChoice === "paper" ? false : true;
} else if (choiceId === "paper") {
userwin = compChoice === "scissors" ? false : true;
} else {
userwin = compChoice === "rock" ? false : true;
}
showwinner(userwin, choiceId, compChoice);
}
};
choices.forEach((choice) => {
console.log(choice);
choice.addEventListener("click", () => {
const choiceId = choice.getAttribute("id");
playgame(choiceId);
});
});