-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoreKeeper.js
65 lines (57 loc) · 1.36 KB
/
scoreKeeper.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
var p1Button = document.querySelector("#p1");
var p2Button = document.getElementById("p2");
var resetButton = document.getElementById("reset");
var p1Score = 0;
var h1= document.querySelector("h1");
var s1 = document.querySelector("#span1");
var s2= document.querySelector("#span2");
var numInput = document.querySelector("input");
var p = document.querySelector("p");
var p2Score = 0;
var gameOver = false;
var winningScore = 5;
// To select a span inside a paragraph
var winninScoreDisplay = document.querySelector("p span");
p1Button.addEventListener("click", function(){
if(!gameOver)
{
p1Score++;
if(p1Score === winningScore)
{
span1.classList.add("winner");
gameOver = true;
}
}
s1.textContent = p1Score;
});
p2Button.addEventListener("click", function(){
if(!gameOver)
{
p2Score++;
if(p2Score === winningScore)
{
span2.classList.add("winner");
gameOver = true;
}
}
s2.textContent = p2Score;
});
resetButton.addEventListener("click", function(){
reset();
});
function reset()
{
p1Score = 0;
p2Score = 0;
span1.textContent = 0;
span2.textContent = 0;
span1.classList.remove("winner");
span2.classList.remove("winner");
gameOver = false;
}
numInput.addEventListener("change", function(){
// instead of numInput.value we can make use of this.value;
winninScoreDisplay.textContent = numInput.value;
winningScore = Number(numInput.value);
reset();
});