-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
49 lines (47 loc) · 1.29 KB
/
app.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
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 = winningScoreSelect.value;
let isGameOver = false;
function updateScore(player, opponent) {
if (!isGameOver) {
player.score++;
player.display.textContent = player.score;
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;
}
}
}
p1.button.addEventListener('click', function() {
updateScore(p1, p2);
});
p2.button.addEventListener('click', function() {
updateScore(p2, p1);
});
winningScoreSelect.addEventListener('change', function() {
winningScore = parseInt(this.value);
reset();
});
resetButton.addEventListener('click', reset);
function reset() {
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;
}
isGameOver = false;
}