-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.js
64 lines (53 loc) · 1.78 KB
/
score.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
var p1Button = document.getElementById("p1");
var p2Button = document.getElementById("p2");
//we need a variable in order to track player 1s and player 2s scroe count
var p1Score = 0;
var p2Score = 0;
//we now need the score to update within the h1 view.
var p1Display = document.querySelector("#p1Display");
var p2Display = document.querySelector("#p2Display");
//we now need to states of the game.
//keep track of the game | game over Boolean states
var gameOver = false;
var winningScore = 5; //has the player reached this vallue?
var resetButton = document.querySelector("#reset");
var numInput = document.querySelector("input");
var winningScoreDisplay = document.querySelector("p span");
//functionality
p1Button.addEventListener("click", function(){
if(!gameOver){
p1Score++;
if(p1Score === winningScore){
p1Display.classList.add("winner");
gameOver = true;
}
p1Display.textContent = p1Score;
}
});
p2Button.addEventListener("click", function(){
if(!gameOver){
p2Score++;
if(p2Score === winningScore){
p2Display.classList.add("winner");
gameOver = true;
}
p2Display.textContent = p2Score;
}
});
resetButton.addEventListener("click", function(){
reset();
});
function reset(){
p1Score = 0;
p2Score = 0;
p1Display.textContent = 0;
p2Display.textContent = 0;
p1Display.classList.remove("winner");
p2Display.classList.remove("winner");
gameOver = false;
}
numInput.addEventListener("change", function(){
winningScoreDisplay.textContent = numInput.value;
winningScore = Number(numInput.value);//this is a weird one, confirming it to number instead of string?
reset();
});