-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs-file.js
More file actions
149 lines (135 loc) · 4.87 KB
/
js-file.js
File metadata and controls
149 lines (135 loc) · 4.87 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
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
140
141
142
143
144
145
146
147
148
149
const choices = ["rock","paper","scissors"];
const winners = [];
let playerInput = [];
let round = 0;
let playerWins = 0;
let computerWins = 0;
const buttons = document.querySelectorAll('.button');
buttons.forEach(button =>{
button.addEventListener('click', () =>{
if (button.id == "rock") {
playerInput.push("rock");
}
else if (button.id == "paper") {
playerInput.push("paper");
}
else if (button.id == "scissors") {
playerInput.push("scissors");
}
round += 1;
clear();
playRound(round);
});
});
function game(){
logWins();
}
function clear(){
if (element = document.getElementById("display")){
element.remove();
}
}
function playRound(round){
const playerSelection = playerInput[0];
const computerSelection = computerChoice();
const winner = checkWinner(playerSelection, computerSelection);
winners.push(winner);
logRound(playerSelection, computerSelection, winner, round);
continuePlaying();
playerInput.pop();
}
function computerChoice() {
return choices[Math.floor(Math.random() * choices.length)];
}
function checkWinner(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return "Tie";
}
else if ((playerSelection === "rock" && computerSelection === "scissors") || (playerSelection === "scissors" && computerSelection === "paper") || (playerSelection === "paper" && computerSelection === "rock")) {
playerWins +=1;
const playerCounter = document.querySelector(".playerScore")
playerCounter.textContent = playerWins;
return "Player";
}
else {
computerWins +=1;
const computerCounter = document.querySelector(".computerScore")
computerCounter.textContent = computerWins;
return "Computer";
}
}
function continuePlaying (){
if (playerWins === 5) {
const body = document.body
let div = document.createElement("div");
div.setAttribute("id","modal-bg-display");
let newDiv = document.createElement("newDiv");
newDiv.setAttribute("class","modal");
div.appendChild(newDiv);
let h1 = document.createElement("h1")
h1.textContent = "Congratulations! You won the game!"
newDiv.appendChild(h1);
let button = document.createElement("button")
button.textContent= "Play again";
button.setAttribute("class", "play-again")
button.addEventListener("click", ()=>{
location.reload();
})
newDiv.appendChild(button);
body.append(div);
}
else if (computerWins === 5) {
const body = document.body
let div = document.createElement("div");
div.setAttribute("id","modal-bg-display");
let newDiv = document.createElement("newDiv");
newDiv.setAttribute("class","modal");
div.appendChild(newDiv);
let h1 = document.createElement("h1")
h1.textContent = "Sorry. You lost the game."
newDiv.appendChild(h1);
let button = document.createElement("button")
button.textContent= "Play again";
button.setAttribute("class", "play-again")
button.addEventListener("click", ()=>{
location.reload();
})
newDiv.appendChild(button);
body.append(div);
}
}
function logWins() {
let playerWins = winners.filter((item) => item == "Player").length;
let computerWins = winners.filter((item) => item = "Computer").length;
let ties = winners.filter((item) => item == "Tie").length;
console.log("Results:");
console.log("Player Wins:", playerWins);
console.log("Computer Wins", computerWins);
console.log("Ties:", ties);
}
function logRound (playerChoice, computerChoice, winner, round) {
if (winner === "Player" || winner === "Computer"){
const body = document.body
const div = document.createElement("div")
div.setAttribute("id","display");
div.style.textAlign = "center";
div.style.marginbottom = "20px";
div.textContent = "Round " + round + ": Player chose " + playerChoice + " and Computer chose " + computerChoice +". " + winner + " won the round!";
body.append(div);
}
else {
const body = document.body
const div = document.createElement("div")
div.setAttribute("id","display");
div.style.textAlign = "center";
div.style.marginbottom = "20px";
div.textContent = "Round " + round + ": Player chose " + playerChoice + " and Computer chose " + computerChoice +". It's a tie!";
body.append(div);
}
console.log("Round:", round);
console.log("Player chose:", playerChoice);
console.log("Computer chose:", computerChoice);
console.log(winner, "won the round");
console.log("-------------------");
}
game()