-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.plurality.html
98 lines (84 loc) · 3.12 KB
/
index.plurality.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Rock, Paper, Scissors</title>
<style>
* {
border-style: solid;
border-color: red;
}
/* Optional: Add some styling to the selected items */
.selected {
background-color: #f0f0f0;
}
</style>
</head>
<body>
<h2>Rock, Paper, Scissors</h2>
<ol id="choices">
<li>rock</li>
<li>paper</li>
<li>scissors</li>
<!-- Add more list items as needed -->
</ol>
<script>
const choices = document.getElementById("choices");
let selectedCount = 0;
// Event listener for list item clicks
choices.addEventListener("click", (event) => {
const listItem = event.target;
const itemText = listItem.textContent;
const itemIndex = Array.from(choices.children).indexOf(listItem);
if (listItem.tagName === "LI") {
if (listItem.classList.contains("selected")) {
// Deselect the item
listItem.classList.remove("selected");
selectedCount--;
console.log("de-selected " + itemIndex + ", " + itemText);
} else if (selectedCount < 2) {
// Select the item (up to 2 selections allowed)
listItem.classList.add("selected");
selectedCount++;
console.log("selected " + itemIndex + ", " + itemText);
} else {
// Overvote
console.log("rejected (overvote) - ignoring " + itemIndex + ", " + itemText);
}
}
});
function getComputerChoice() {
let choices = ["rock", "paper", "scissor"];
return choices[(Math.floor(Math.random() * choices.length))]
}
function playRound(playerSelection, computerSelection) {
let playerSel = playerSelection.toLowerCase();
let computerSel = computerSelection.toLowerCase();
if (playerSel == computerSel) {
return "Tie - both choose " + playerSel
}
if (playerSel == "rock") {
if (computerSel == "paper") {
return "you lose (paper beats rock"
}
return "you win (rock beats scissors)"
}
if (playerSel == "paper") {
if (computerSel == "rock") {
return "you win (paper beats rock)"
}
return "you lose (scissors beats paper)"
}
if (playerSel == "scissors") {
if (computerSel == "rock") {
return "you lose (rock beats scissors)"
}
return "you win (scissors beats paper)"
}
}
const playerSelection = "rock";
const computerSelection = getComputerChoice();
console.log(playRound(playerSelection, computerSelection));
</script>
</body>
</html>