forked from WhalenSITHS/Algorithm-Practice-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKenneth.js
More file actions
145 lines (127 loc) · 3.05 KB
/
Kenneth.js
File metadata and controls
145 lines (127 loc) · 3.05 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
const DOMSelectors = {
content: document.querySelector(".content"),
rock: document.querySelector("#rock"),
paper: document.querySelector("#paper"),
scissors: document.querySelector("#scissors"),
historyButton: document.querySelector("#history"),
history: document.querySelector(".history"),
};
const history = [];
function returnInput(input) {
let output;
if (input == 0) {
output = "rock";
} else if (input == 1) {
output = "paper";
} else {
output = "scissors";
}
return output;
}
function rockPaperScissors(input) {
const random = Math.floor(Math.random() * 100);
let opponentChoice;
let outcome;
for (let i = 0; i <= random; i++) {
if (i % 3 == 0) {
opponentChoice = "rock";
} else if (i % 2 == 0) {
opponentChoice = "paper";
} else {
opponentChoice = "scissors";
}
}
if (opponentChoice == "rock") {
if (input == 1) {
outcome = "win";
} else if (input == 2) {
outcome = "lose";
} else {
outcome = "draw";
}
} else if (opponentChoice == "paper") {
if (input == 2) {
outcome = "win";
} else if (input == 0) {
outcome = "lose";
} else {
outcome = "draw";
}
} else {
if (input == 0) {
outcome = "win";
} else if (input == 1) {
outcome = "lose";
} else {
outcome = "draw";
}
}
history.push(outcome);
DOMSelectors.history.innerHTML = "";
DOMSelectors.content.insertAdjacentHTML(
"afterbegin",
`<div class="card" id="${outcome}">
<p>Your choice: ${returnInput(input).toUpperCase()}</p>
<p>Opponent choice: ${opponentChoice.toUpperCase()}</p>
<h4>${outcome.toUpperCase()}</h4>
</div>
`
);
}
function getHistory() {
DOMSelectors.content.innerHTML = "";
DOMSelectors.history.innerHTML = "";
let win = 0;
let lose = 0;
let draw = 0;
for (let i = 0; i < history.length; i++) {
if (history[i] == "win") {
win++;
} else if (history[i] == "lose") {
lose++;
} else {
draw++;
}
DOMSelectors.history.insertAdjacentHTML(
"afterbegin",
`<div class="historyCard" id="${history[i]}">
<h4>Game ${i + 1}: ${history[i].toUpperCase()}</h4>
</div>`
);
}
DOMSelectors.history.insertAdjacentHTML(
"afterbegin",
`<h2>Win Rate: ${((win / (win + lose + draw)) * 100)
.toString()
.substring(0, 5)}%</h2>
<h3>Wins: ${win} | Losses: ${lose} | Draws: ${draw}</h3>`
);
}
DOMSelectors.rock.addEventListener("click", function () {
rockPaperScissors(0);
});
DOMSelectors.paper.addEventListener("click", function () {
rockPaperScissors(1);
});
DOMSelectors.scissors.addEventListener("click", function () {
rockPaperScissors(2);
});
DOMSelectors.historyButton.addEventListener("click", function () {
getHistory();
});
/* function factorsForN(n) {
if (n <= 0) {
return "negative number or 0";
}
let factors = 1;
for (let i = 1; i <= n; i++) {
factors *= i;
}
return factors;
}
console.log(factorsForN(4)); */
/* function getUserScore(user) {
if (!user) {
return;
}
} */