-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (47 loc) · 1.33 KB
/
index.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
const p1 = {
score: 0,
button: document.querySelector("#p1Button"),
display: document.querySelector("#p1Display"),
};
const p2 = {
score: 0,
button: document.querySelector("#p2Button"),
display: document.querySelector("#p2Display"),
};
const resetButton = document.querySelector("#reset");
const winningScoreSelect = document.querySelector("#playto");
let winningScore = 3;
let isGameOver = false;
function updateScores(player, opponent) {
if (!isGameOver) {
player.score += 1;
if (player.score === winningScore) {
isGameOver = true;
player.display.classList.add("has-text-success");
opponent.display.classList.add("has-text-danger");
player.button.disabled = true;
opponent.button.disabled = true;
}
player.display.textContent = player.score;
}
}
p1.button.addEventListener("click", function () {
updateScores(p1, p2);
});
p2.button.addEventListener("click", function () {
updateScores(p2, p1);
});
winningScoreSelect.addEventListener("change", function () {
winningScore = parseInt(this.value);
reset();
});
resetButton.addEventListener("click", reset);
function reset() {
isGameOver = false;
for (let p of [p1, p2]) {
p.score = 0;
p.display.textContent = 0;
p.display.classList.remove("has-text-success", "has-text-danger");
p.button.disabled = false;
}
}