-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
93 lines (91 loc) · 3.48 KB
/
script.js
File metadata and controls
93 lines (91 loc) · 3.48 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var playingTo = 0;
var player1Score = 0;
var player2Score = 0;
var gameOver = false;
// Use Selector methods to select the HTML elements needed below.
var scoreSetButton = document.querySelector("#score-set-button");
var playingToField = document.querySelector("#playing-to-field");
var setScoreTitle = document.querySelector("#set-score-title");
var resetButton = document.querySelector("#reset-button");
// p1 fields
var p1ScoreDisplay = document.querySelector("#p1-score");
var p1ScoreButton = document.querySelector("#add-p1-point");
// p2 fields
var p2ScoreDisplay = document.querySelector("#p2-score");
var p2ScoreButton = document.querySelector("#add-p2-point");
// This function sets the score the game is played to.
// It should:
// capture the value the user types into the form field
// Set the score title to "Playing to X." For whatever X the user types into the box
var setPlayingTo = function() {
playingTo = Number(playingToField.value); // captures the value the user types into the field as a string
console.log(playingTo);
if(playingTo > 0) {
setScoreTitle.textContent = "Yo yo we're playing to " + playingTo;
} else {
setScoreTitle.textContent = "You need to play to a positive number"
}
}
// This function adds a point to Player 1.
// It should:
// increment player1Score
// update the display for player 1
// check to see if a player has won the game
// Do nothing if the winning score has not yet been set, or if the game is over.
var addPlayerOnePoint = function() {
if (gameOver === false) {
player1Score += 1;
console.log(player1Score);
p1ScoreDisplay.textContent = player1Score;
if(player1Score >= playingTo) {
setScoreTitle.textContent = "Player 1 wins!";
gameOver = true;
p1ScoreDisplay.style.color = "green";
}
else {
setScoreTitle.textContent = "Player 1 scores";
}
}
}
// This function adds a point to Player 2.
// It should work the same as the function above, but for Player 2
var addPlayerTwoPoint = function() {
if(gameOver === false) {
player2Score += 1;
console.log(player2Score);
p2ScoreDisplay.textContent = player2Score;
if(player2Score >= playingTo) {
setScoreTitle.textContent = "Player 2 wins!";
gameOver = true;
p1ScoreDisplay.style.color = "green";
} else {
setScoreTitle.textContent = "Player 2 scores";
}
}
}
// This function checks to see if a player has won.
// If a player has reached the end score, it should:
// set the score display to green for the winning player
// set the score title to "Player X has won!"
// set gameOver to true
// This function resets the Game back to a zero state
// It should:
// reset both player scores to 0
// set the score title back to "What are we playing to?"
// set gameOver back to false if it is true
var resetGame = function() {
player1Score = 0;
player2Score = 0;
playingTo = 0;
gameOver = false;
setScoreTitle.textContent = "New game!";
p1ScoreDisplay.textContent = player1Score;
p2ScoreDisplay.textContent = player2Score;
p1ScoreDisplay.style.color = "black";
p2ScoreDisplay.style.color = "";
}
// Add Event Listeners to buttons and HTML elements
scoreSetButton.addEventListener("click", setPlayingTo);
p1ScoreButton.addEventListener("click", addPlayerOnePoint);
p2ScoreButton.addEventListener("click", addPlayerTwoPoint);
resetButton.addEventListener("click", resetGame);