-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarter.js
67 lines (59 loc) · 1.94 KB
/
starter.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const p1 = {
name: 'PLAYER ONE',
score: 0,
button: document.querySelector('#p1Button'),
display: document.querySelector('#p1Display')
};
const p2 = {
name: 'PLAYER TWO',
score: 0,
button: document.querySelector('#p2Button'),
display: document.querySelector('#p2Display')
};
const resetButton = document.querySelector('#reset');
const winningScoreSelect = document.querySelector('#playTo');
let winningScore = parseInt(winningScoreSelect.value);
let isGameOver = false;
let outcome = document.createElement('p');
outcome.classList.add('subtitle');
document.querySelector('div.content').prepend(outcome);
function updateScores(player, opponent) {
if (!isGameOver) {
player.score++;
player.display.textContent = player.score;
outcome.textContent = '';
if (player.score === winningScore - 1) {
if (opponent.score === winningScore - 1) {
outcome.textContent = 'DEUCE!';
winningScore += 1;
}
}
if (player.score === winningScore) {
outcome.textContent = `${player.name} WON!!!`;
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 () {
updateScores(p1, p2);
});
p2.button.addEventListener('click', function () {
updateScores(p2, p1);
});
resetButton.addEventListener('click', reset);
winningScoreSelect.addEventListener('change', reset);
function reset() {
isGameOver = false;
winningScore = parseInt(winningScoreSelect.value);
outcome.textContent = '';
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;
}
};