-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (52 loc) · 1.5 KB
/
script.js
File metadata and controls
59 lines (52 loc) · 1.5 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
var plButton = document.getElementById("p1");
var p1Display = document.querySelector("#p1Display");
var p1Score = 0;
var p2Button = document.getElementById("p2");
var p2Display = document.querySelector("#p2Display");
var p2Score = 0;
var resetButton = document.getElementById("reset");
var numInput = document.querySelector("input");
var p = document.querySelector("p");
var winningScoreDisplay = document.querySelector("p span");
var winningScore = 5;
var gameOver = false;
// player one
plButton.addEventListener("click", function () {
if (!gameOver) {
p1Score++;
if (p1Score === winningScore) {
gameOver = true;
p1Display.style.color = "green";
}
p1Display.textContent = p1Score;
}
});
// player Two
p2Button.addEventListener("click", function () {
if (!gameOver) {
p2Score++;
if (p2Score === winningScore) {
gameOver = true;
p2Display.style.color = "green";
}
p2Display.textContent = p2Score;
}
});
resetButton.addEventListener("click", function () {
reset();
});
// event to update winning score when score changes
// event to update input value when score changes
function reset() {
p1Display.textContent = 0;
p2Display.textContent = 0;
p1Display.style.color = "black";
p2Display.style.color = "black";
gameOver = false;
}
numInput.addEventListener("change", function () {
// whatever the input has as value, it will be displayed on p
winningScoreDisplay.textContent = numInput.value;
winningScore = Number(numInput.value);
reset();
});