-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
64 lines (57 loc) · 2.15 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
let userScore = 0;
let compScore = 0;
let userChoice;
const choices = document.querySelectorAll(".choice");
const msg = document.querySelector("#msg");
const userScoreCount = document.querySelector("#user-score");
const compScoreCount = document.querySelector("#comp-score");
const gnrtCompChoice = () => { //Generating choice of the computer.
const options = ["rock", "paper", "scissors"];
const randomIdx = Math.floor(Math.random() * 3);
return options[randomIdx];
};
const showWinner = (userWin, userChoice, compChoice) => {
if (userWin) {
userScore++;
userScoreCount.innerText = userScore;
msg.innerText = `Congrats, you won! Computer chose ${compChoice}.`;
msg.style.backgroundColor = "rgb(119,199,26)";
msg.style.color = "white";
} else {
compScore++;
compScoreCount.innerText = compScore;
msg.innerText = `Oops, you lost! Computer chose ${compChoice}.`
msg.style.backgroundColor = "#ff3535";
msg.style.color = "white";
}
};
const playGame = (userChoice) => {
console.log("user choice = ", userChoice);
const compChoice = gnrtCompChoice();
console.log("comp choice =", compChoice);
const gameDrawn = () => {
console.log("game was drawn.")
msg.innerText = `Game drawn! You both chose ${userChoice}.`
msg.style.backgroundColor = "#ffd100";
msg.style.color = "#202020";
};
if (userChoice === compChoice) {
gameDrawn(userChoice); // Pass userChoice to gameDrawn
} else {
let userWin = true;
if(userChoice === "rock") { //paper or scissors
userWin = compChoice === "paper" ? false : true;
} else if(userChoice === "paper") { //rock or scissors
userWin = compChoice === "scissors" ? false : true;
} else { //rock or scissors
userWin = compChoice === "rock" ? false : true;
};
showWinner(userWin, userChoice, compChoice); // Pass userWin instead of userChoice
}
};
choices.forEach((choice) => {
choice.addEventListener("click", () => {
const userChoice = choice.getAttribute("id");
playGame(userChoice);
});
});