-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
71 lines (66 loc) · 2.37 KB
/
Copy pathapp.js
File metadata and controls
71 lines (66 loc) · 2.37 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
let userScore = 0;
let comScore = 0;
const choices = document.querySelectorAll(".choice");
const msg = document.querySelector("#msg");
const userScorePara = document.querySelector("#user-score");
const compScorePara = document.querySelector("#comp-score");
const genCompChoice =() => {
const options =[" rock", "paper","scissors"];
const randIdx = Math.floor(Math.random()*3);
return options[randIdx]
};
const drawGame =() =>{
console.log(" game was draw.");
msg.innerText ="Game was draw. Play again.";
msg.style.backgroundColor = "#081b31";
};
const showWinner =(userWin , userChoice , compChoice) =>{
if(userWin) {
console.log("you win!");
msg.innerText =`You win! Your ${userChoice} beats ${compChoice}`;
msg.style.backgroundColor = "#1BC74F";
}else{
console.log("you Lose");
msg.innerText =`You lose. ${compChoice} beats Your ${userChoice}`;
msg.style.backgroundColor = "#cf1717";
}
};
const playGame = (userChoice) => {
console.log("User choice =", userChoice);
const compChoice = genCompChoice();
console.log("Computer choice =", compChoice);
if (userChoice === compChoice) {
drawGame();
console.log(`Both chose ${userChoice}. It's a draw!`);
} else {
let userWin;
switch (userChoice) {
case "rock":
userWin = (compChoice === "scissors");
break;
case "paper":
userWin = (compChoice === "rock");
break;
case "scissors":
userWin = (compChoice === "paper");
break;
}
showWinner(userWin , userChoice , compChoice);
if (userWin) {
userScore++;
console.log("Current Score - You: " + userScore + " | Computer: " + comScore);
userScorePara.innerText = userScore;
} else {
comScore++;
console.log("Current Score - You: " + userScore + " | Computer: " + comScore);
compScorePara.innerText = comScore;
}
}
};
choices.forEach((choice) => {
// console.log(choice);
choice.addEventListener("click", () => {
const userChoice = choice.getAttribute("id");
playGame(userChoice);
});
});