skip to Main Content

Im a begginer to javascript and im doing a simple ping pong score keeper. The thing here is that i want to dissable ‘buttonOne’ and ‘buttonTwo’ whenever ‘playerOneScore’ or ‘playerTwoScore’ arrives at the value of the select tag (‘scoreLimit’). I tried using an integer like 7 on the if tag and it seems to work like that, so im guessing its probably an issue with the variable ‘scoreLimit’ scope or something like that. I am also having an issue when the scoreOne or scoreTwo arrives at the limit, as i want the buttons to dissable just when the latter happens, but instead i got to press it one more time for it to work.

HTML

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>scoreKeeper - JS</title>
    <link rel="stylesheet" href="scoreKeeper.css">
    <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">

</head>

<body>

    <div id="imageDiv">
        <img src="https://media.istockphoto.com/id/503250996/photo/ping-pong-or-table-tennis-background-with-rackets.jpg?s=612x612&w=0&k=20&c=XSTF2456Iq81RxL_D887ikZOpyfEtUgEpPZGfTKrwRI="
            alt="tableTennisImage">
    </div>

    <div id="title">
        <h3>Ping Pong Score Keeper</h3>
    </div>

    <div id="scoreDiv">
        <div id="scoreKeep">
            <h1 id="uno">0</h1>
            <h1 id="dos">to</h1>
            <h1 id="tres">0</h1>
        </div>

        <p>Use the buttons below to keep the score</p>
        <label for="limitOptions" id="limiterLabel">Playing To</label>
        <select name="limitOptions" id="scoreLimiter">
            <option value="7">7</option>
            <option value="11">11</option>
            <option value="21">21</option>
        </select>
        <div id="buttons">
            <button id="sumPlayerOne">1+ Player One</button>
            <button id="sumPlayerTwo">1+ Player Two</button>
            <button id="reset">Reset</button>
        </div>
    </div>

    <script src="scoreKeeper.js"></script>
</body>

</html>

JAVASCRIPT

const buttonOne = document.querySelector("#sumPlayerOne");
const buttonTwo = document.querySelector("#sumPlayerTwo");
const resetear = document.querySelector("#reset")
const playerOneScore = document.querySelector("#uno")
let scoreLimit = document.querySelector("#scoreLimiter").value;
const playerTwoScore = document.querySelector("#tres")

let scoreOne = 0;
let scoreTwo = 0;

buttonOne.addEventListener("click", (e) => {
     scoreLimit = document.querySelector("#scoreLimiter").value;
    if (scoreOne === scoreLimit) {
        buttonTwo.disabled = true;
        buttonOne.disabled = true;
        document.getElementById("uno").style.color = "green";
        document.getElementById("tres").style.color = "red";

    }
    else {
        const winOne = () => {

            scoreOne = scoreOne + 1;
            playerOneScore.innerText = `${scoreOne}`
        }
        winOne()
    }
});

buttonTwo.addEventListener("click", (e) => {
     scoreLimit = document.querySelector("#scoreLimiter").value;
    console.log(scoreLimit)
    if (scoreTwo === scoreLimit) {
        buttonTwo.disabled = true;
        buttonOne.disabled = true;
        document.getElementById("uno").style.color = "red"
        document.getElementById("tres").style.color = "green"
    }
    else {
        const winTwo = () => {
            scoreTwo = scoreTwo + 1;
            playerTwoScore.innerText = `${scoreTwo}`
        }
        winTwo()
    }
});

resetear.addEventListener("click", (e) => {
    scoreOne = 0
    scoreTwo = 0
    playerOneScore.innerText = `${scoreOne}`
    playerTwoScore.innerText = `${scoreTwo}`
    buttonOne.disabled = false;
    buttonTwo.disabled = false;
    document.getElementById("uno").style.color = "black"
    document.getElementById("tres").style.color = "black"
})

CSS

* {
    box-sizing: border-box;
}

body{
    font-family: roboto;
    font-weight: 600;
}

#imageDiv {
display: flex;
justify-content: center;
}

img {
width: 25rem;
border-radius: 10px;
}

#title{
    margin-top: 1rem;
    margin-bottom: 10px;
    padding-left: 1rem;
    border: 1px solid lightgray;
    margin-left: 5px;
    margin-right: 5px;
    border-radius: 10px ;
}

#scoreDiv {
border: 1px solid lightgray;
padding-left: 1rem;
padding-bottom: 1rem;
border-radius: 10px ;
margin-left: 5px;
margin-right: 5px;
}

#scoreLimiter{
    font-family: roboto;
    font-size: 17px;
}

#sumPlayerOne{
    font-family: roboto;
}

#sumPlayerTwo{
    font-family: roboto;
}

#reset{
    font-family: roboto;
}

#limiterLabel{
font-size: 20px;
} 

#buttons{
    margin-top: 1rem;
}

#scoreKeep{
    display: flex;

    font-size: 20px;
}
#uno{
    transition: .1s;
    margin-right: 1.33rem;
    
}

#dos{
    transition: .1s; 
    margin-right: 1.33rem;
}

#tres{
    transition: .1s;
}

2

Answers


  1. You can just use a function to check wether the score of each player has reached the limit and call the function everytime you sum the score like this

    const buttonOne = document.querySelector("#sumPlayerOne");
    const buttonTwo = document.querySelector("#sumPlayerTwo");
    const resetear = document.querySelector("#reset")
    const playerOneScore = document.querySelector("#uno")
    let scoreLimit = document.querySelector("#scoreLimiter").value;
    const playerTwoScore = document.querySelector("#tres")
    
    let scoreOne = 0;
    let scoreTwo = 0;
    
    buttonOne.addEventListener("click", (e) => {
        scoreLimit = document.querySelector("#scoreLimiter").value;
        
        const winOne = () => {
    
          scoreOne = scoreOne + 1;
          playerOneScore.innerText = `${scoreOne}`
        }
        winOne()
        checkLimit(scoreOne, 1);
        
    });
    
    buttonTwo.addEventListener("click", (e) => {
        scoreLimit = document.querySelector("#scoreLimiter").value;
        
        const winTwo = () => {
          scoreTwo = scoreTwo + 1;
          playerTwoScore.innerText = `${scoreTwo}`
        }
        winTwo()
        checkLimit(scoreTwo, 2);
        
    });
    
    resetear.addEventListener("click", (e) => {
        scoreOne = 0
        scoreTwo = 0
        playerOneScore.innerText = `${scoreOne}`
        playerTwoScore.innerText = `${scoreTwo}`
        buttonOne.disabled = false;
        buttonTwo.disabled = false;
        document.getElementById("uno").style.color = "black"
        document.getElementById("tres").style.color = "black"
    })
    
    function checkLimit(score, player){
        scoreLimit = document.querySelector("#scoreLimiter").value;
        if(score == scoreLimit){
            buttonTwo.disabled = true;
            buttonOne.disabled = true;
            let player1 = "green";
            let player2 = "red";
            if(player==2){
              player1 = "red";
              player2 = "green";
            }
            document.getElementById("uno").style.color = player1;
            document.getElementById("tres").style.color = player2;
      }
    
    }
    * {
      box-sizing: border-box;
    }
    
    body {
      font-family: roboto;
      font-weight: 600;
    }
    
    #imageDiv {
      display: flex;
      justify-content: center;
    }
    
    img {
      width: 25rem;
      border-radius: 10px;
    }
    
    #title {
      margin-top: 1rem;
      margin-bottom: 10px;
      padding-left: 1rem;
      border: 1px solid lightgray;
      margin-left: 5px;
      margin-right: 5px;
      border-radius: 10px;
    }
    
    #scoreDiv {
      border: 1px solid lightgray;
      padding-left: 1rem;
      padding-bottom: 1rem;
      border-radius: 10px;
      margin-left: 5px;
      margin-right: 5px;
    }
    
    #scoreLimiter {
      font-family: roboto;
      font-size: 17px;
    }
    
    #sumPlayerOne {
      font-family: roboto;
    }
    
    #sumPlayerTwo {
      font-family: roboto;
    }
    
    #reset {
      font-family: roboto;
    }
    
    #limiterLabel {
      font-size: 20px;
    }
    
    #buttons {
      margin-top: 1rem;
    }
    
    #scoreKeep {
      display: flex;
      font-size: 20px;
    }
    
    #uno {
      transition: .1s;
      margin-right: 1.33rem;
    }
    
    #dos {
      transition: .1s;
      margin-right: 1.33rem;
    }
    
    #tres {
      transition: .1s;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>scoreKeeper - JS</title>
      <link rel="stylesheet" href="scoreKeeper.css">
      <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
    
    </head>
    
    <body>
    
      <div id="imageDiv">
        <img src="https://media.istockphoto.com/id/503250996/photo/ping-pong-or-table-tennis-background-with-rackets.jpg?s=612x612&w=0&k=20&c=XSTF2456Iq81RxL_D887ikZOpyfEtUgEpPZGfTKrwRI=" alt="tableTennisImage">
      </div>
    
      <div id="title">
        <h3>Ping Pong Score Keeper</h3>
      </div>
    
      <div id="scoreDiv">
        <div id="scoreKeep">
          <h1 id="uno">0</h1>
          <h1 id="dos">to</h1>
          <h1 id="tres">0</h1>
        </div>
    
        <p>Use the buttons below to keep the score</p>
        <label for="limitOptions" id="limiterLabel">Playing To</label>
        <select name="limitOptions" id="scoreLimiter">
          <option value="7">7</option>
          <option value="11">11</option>
          <option value="21">21</option>
        </select>
        <div id="buttons">
          <button id="sumPlayerOne">1+ Player One</button>
          <button id="sumPlayerTwo">1+ Player Two</button>
          <button id="reset">Reset</button>
        </div>
      </div>
    
      <script src="scoreKeeper.js"></script>
    </body>
    
    </html>
    Login or Signup to reply.
  2. First increase the score, then check for the win, also you were using triple equals === to compare between an integer and the value of an input which is a string, this always returns false because === checks for the value and type, to compare you have to use 2 equals == or convert the string to int with a parseInt

     const buttonOne = document.querySelector("#sumPlayerOne");
          const buttonTwo = document.querySelector("#sumPlayerTwo");
          const resetear = document.querySelector("#reset");
          const playerOneScore = document.querySelector("#uno");
          let scoreLimit = document.querySelector("#scoreLimiter").value;
          const playerTwoScore = document.querySelector("#tres");
    
          let scoreOne = 0;
          let scoreTwo = 0;
    
          buttonOne.addEventListener("click", (e) => {
            scoreOne++;
            playerOneScore.innerHTML = scoreOne;
    
            if (scoreOne === parseInt(scoreLimit)) {
              buttonTwo.disabled = true;
              buttonOne.disabled = true;
              document.getElementById("uno").style.color = "green";
              document.getElementById("tres").style.color = "red";
            }
          });
    
          buttonTwo.addEventListener("click", (e) => {
            scoreTwo++;
            playerTwoScore.innerHTML = scoreTwo;
    
            if (scoreTwo === parseInt(scoreLimit)) {
              buttonTwo.disabled = true;
              buttonOne.disabled = true;
              document.getElementById("uno").style.color = "red";
              document.getElementById("tres").style.color = "green";
            }
          });
    
          resetear.addEventListener("click", (e) => {
            scoreOne = 0;
            scoreTwo = 0;
            playerOneScore.innerText = `${scoreOne}`;
            playerTwoScore.innerText = `${scoreTwo}`;
            buttonOne.disabled = false;
            buttonTwo.disabled = false;
            document.getElementById("uno").style.color = "black";
            document.getElementById("tres").style.color = "black";
          });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search