Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body>
    <div class="score-display"><span id="score1">0</span> - <span id="score2">0</span></div>
    <p>Playing to: <span id="maxScoreDisplay">5</span></p>

       <label for="max-score-input">Choose Final Score (1-10):</label>
       <input type="number" id="maxScoreInput" name="max-score-input" value="5" min="1" max="10">
       
       <button id="player1button">Player 1</button>
       <button id="player2button">Player 2</button>
       <button id="reset">Reset</button>
</body>
              
            
!

CSS

              
                body {
    font: 1rem 'Fira Sans', sans-serif; 
}

label {
    display: block;
}

#score1, #score2 {
    
}

.score-display {
    font-size: 6em;
}

#maxScoreDisplay {
    font-size: 2em;
}

.turnGreen {
    color: green;
}


              
            
!

JS

              
                // PLAYER VARIABLES
// Assigning variables to each player button
var p1Button = document.querySelector("#player1button");
var p2Button = document.querySelector("#player2button");

// Assigning variables to Score display <span>s
var p1Display = document.querySelector("#score1");
var p2Display = document.querySelector("#score2");

// Creating variables to track current score
var p1Score = Number(document.querySelector("#score1").textContent);
var p2Score = Number(document.querySelector("#score2").textContent);

// MAX SCORE VARIABLES
var maxScoreInput = document.querySelector("#maxScoreInput");
var maxScoreDisplay = document.querySelector("#maxScoreDisplay");
var maxScore = Number(maxScoreInput.value);

// RESET VARIABLES
var resetButton = document.querySelector("#reset");

// EVENT LISTENERS
// Simply for debugging in the console
p1Button.addEventListener("click", logClick);
p2Button.addEventListener("click", logClick);

// clicking player buttons adds 1 to their score
p1Button.addEventListener("click", p1PlusOne);
p2Button.addEventListener("click", p2PlusOne);

// set max score display = to max score input
maxScoreInput.addEventListener("change", changeMaxScore);

// reset button sets scores back to zero and removes green class
resetButton.addEventListener("click", resetScore);


// FUNCTIONS
// console.log function for click event listeners
function logClick() {
  console.log("Button has been clicked");
}

// **The following two functions could be refactored together** 
// if neither player is at max score, gives 1 point to player 1, if max score is reached, turns score display green
function p1PlusOne() {
  if (p1Score < maxScore && p2Score < maxScore) {
    p1Score++;
    console.log("player 1 score is " + p1Score);
    p1Display.textContent = p1Score;
  }
  if (p1Score === maxScore) {
    p1Display.classList.add("turnGreen");
  }
}

// if neither player is at max score, gives 1 point to player 2, if max score is reached, turns score display green
function p2PlusOne() {
  if (p1Score < maxScore && p2Score < maxScore) {
    p2Score++;
    console.log("player 2 score is " + p2Score);
    p2Display.textContent = p2Score;
  }
  if (p2Score === maxScore) {
    p2Display.classList.add("turnGreen");
  }
}

// updates the max score display with the current max score value
function changeMaxScore() {
  maxScore = Number(maxScoreInput.value);
  maxScoreDisplay.textContent = maxScore;
}

// resets the player scores to zero, sets player score displays to new score which is zero, removes .turnGreen CSS class
function resetScore() {
  p1Score = 0;
  p2Score = 0;
  p1Display.textContent = p1Score;
  p2Display.textContent = p2Score;
  p1Display.classList.remove("turnGreen");
  p2Display.classList.remove("turnGreen");
}


              
            
!
999px

Console