-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
73 lines (63 loc) · 1.8 KB
/
app.js
File metadata and controls
73 lines (63 loc) · 1.8 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
let gameChoices = [
{
title: "rock",
winsAgainst: ["scissors", "lizard"],
},
{
title: "paper",
winsAgainst: ["rock", "spock"],
},
{
title: "scissors",
winsAgainst: ["paper", "lizard"],
},
{
title: "lizard",
winsAgainst: ["paper", "spock"],
},
{
title: "spock",
winsAgainst: ["scissors", "rock"],
},
];
let playerWins = 0;
let playerLosses = 0;
let playDesc = "";
function play(playerChoice) {
// Determine computer choice
let computerChoice =
gameChoices[Math.floor(Math.random() * gameChoices.length)].title;
// loop over the choices array to find the object that matches the input of the player
let playerObject = gameChoices.find(
(c) => c.title.toLowerCase() == playerChoice.toLowerCase()
);
let outcome = "";
if (playerObject.winsAgainst.includes(computerChoice)) {
outcome = "win!";
playerWins += 1;
} else if (playerObject.title == computerChoice) {
outcome = "tie. gg fren.";
} else {
outcome = "lose. :(";
playerLosses += 1;
}
document.querySelector(
"#computer-choice"
).innerHTML = `The computer threw ${computerChoice}. You ${outcome} <br>Click a button to play again!`;
drawWinLoss();
}
// Much later: I think that using the title of iten as both what's shown as the button and what gets passed to play() is at least mildly clever
function drawButtons() {
let template = "";
gameChoices.forEach((item) => {
template += `<button type="button" class="btn btn-secondary" data-toggle="button" onclick="play('${item.title}')">${item.title}</button>`;
});
document.querySelector("#button-col").innerHTML = template;
}
function drawWinLoss() {
document.querySelector(
"#win-loss"
).innerHTML = `Your wins: ${playerWins} <br> Your losses: ${playerLosses}`;
}
drawButtons();
drawWinLoss();