-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
139 lines (113 loc) · 3.88 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
function getComputerChoice() {
let computerChoice = Math.floor(Math.random() * 3 + 1);
computerChoice = computerChoice.toString();
computerChoice == "1"
? (computerChoice = "rock")
: computerChoice == "2"
? (computerChoice = "paper")
: computerChoice == "3"
? (computerChoice = "scissors")
: "";
return computerChoice;
}
let humanScore = 0;
let computerScore = 0;
const pComputerScore = document.createElement("p");
const pHumanScore = document.createElement("p");
const message = document.createElement("div");
message.innerText = "Rock Paper Scissors \n Start!!";
const pHumanChoice = document.createElement("p");
const pComputerChoice = document.createElement("p");
function playRound(humanChoice, computerChoice = getComputerChoice()) {
pHumanChoice.innerText = "You :" + humanChoice;
pComputerChoice.innerText = "Computer :" + computerChoice;
if (humanChoice == computerChoice) {
message.innerText = `It\'s a TIE!!!! ¯\\_(ツ)_/¯`;
} else if (humanChoice == "rock" && computerChoice == "scissors") {
message.innerText = `You Win!!! ヾ(⌐■_■)ノ`;
humanScore++;
} else if (humanChoice == "paper" && computerChoice == "rock") {
message.innerText = `You Win!!! ヾ(⌐■_■)ノ`;
humanScore++;
} else if (humanChoice == "scissors" && computerChoice == "paper") {
message.innerText = `You Win!!! ヾ(⌐■_■)ノ`;
humanScore++;
} else {
message.innerText = `You Lost!!! (ノಠ益ಠ)ノ彡┻━┻`;
computerScore++;
}
pComputerScore.innerText = "Computer :" + computerScore;
pHumanScore.innerText = "You :" + humanScore;
if (computerScore == 5 || humanScore == 5) {
message.innerText =
"Game Over || Winner is : " +
winner() +
" with 5 scores!!. \n Start Again!!";
humanScore = 0;
computerScore = 0;
pComputerScore.innerText = "Computer :" + computerScore;
pHumanScore.innerText = "You :" + humanScore;
}
}
function winner() {
if (computerScore == humanScore) {
return "No one";
} else if (computerScore > humanScore) {
return "Computer";
} else {
return "Human";
}
}
const rockBtn = document.createElement("button");
const paperBtn = document.createElement("button");
const scissorsBtn = document.createElement("button");
rockBtn.innerText = "Rock";
paperBtn.innerText = "Paper";
scissorsBtn.innerText = "Scissors";
rockBtn.addEventListener("click", () => {
playRound("rock");
});
paperBtn.addEventListener("click", () => {
playRound("paper");
});
scissorsBtn.addEventListener("click", () => {
playRound("scissors");
});
const div = document.querySelector("div");
const divChoice = document.createElement("div");
const divButtons = document.createElement("div");
const divScore = document.createElement("div");
divChoice.appendChild(pHumanChoice);
divChoice.appendChild(pComputerChoice);
divScore.appendChild(pHumanScore);
divScore.appendChild(pComputerScore);
divButtons.appendChild(rockBtn);
divButtons.appendChild(paperBtn);
divButtons.appendChild(scissorsBtn);
div.appendChild(message);
div.appendChild(divChoice);
div.appendChild(divButtons);
div.appendChild(divScore);
//styling
message.style.fontSize = "25px";
message.style.textAlign = "center";
message.style.padding = "50px";
divChoice.style.background = "lightgrey";
divChoice.style.display = "flex";
divChoice.style.justifyContent = "space-around";
divChoice.style.borderRadius = "50px";
divChoice.style.padding = "30px";
divChoice.style.fontSize = "25px";
divButtons.style.display = "flex";
divButtons.style.justifyContent = "space-around";
divButtons.style.margin = "20px";
const allBtn = document.querySelectorAll("div button");
allBtn.forEach((button) => {
button.style.flexGrow = "1";
button.style.padding = "50px";
button.style.borderRadius = "50px";
button.style.fontSize = "20px";
});
divScore.style.display = "flex";
divScore.style.justifyContent = "space-around";
divScore.style.fontSize = "20px";