Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
How do I update a Scoreboard on my HTML page

I'm trying to build a rock scissors paper game And I need to add a scoreboard. So I need the score to increase by 1 every time the player or the computer win a round. How do I get it to do this?

Here's the code I wrote but it is not updating the scoreboard on the screen

  function scoreboard() {
    computerScore = parseInt(computerScore.textContent);
    playerScore = parseInt(playerScore.textContent);

    if(playerScore < 5 || computerScore < 5) {
      if(playRound(playerSelection, computerSelection) == 'You win! Paper beats Rock' || playRound(playerSelection, computerSelection) == 'You win! Rock beats Scissors' || playRound(playerSelection, computerSelection) == 'You win! Scissors beats Paper') {
        return playerScore += 1
      } else if(playRound(playerSelection, computerSelection) == 'You lose! Paper beats Rock' || playRound(playerSelection, computerSelection) == 'You lose! Rock beats Scissors' || playRound(playerSelection, computerSelection) == 'You lose! Scissors beats Paper') {
        return computerScore += 1
      } 
    } else if(playerScore == 5) {
      console.log('Congratulations!');
    } else if(computerScore == 5) {
      console.log('You lose!');
    }

  }

Here's is the Html:

  <div class="score">
    <p> Score Board: </p>
    <div class="board">
      <P> Computer: <span class="computer__score"> 0 </span></P>
      <p> Player: <span class="player__score"> 0 </span></p>
    </div>
  </div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can define a function for updating the scoreboard and update text content in it:

function updateScoreboard(computerScore, playerScore) {
    document.querySelector('.computer_score').textContent = computerScore;
    document.querySelector('.player_score').textContent = playerScore;
}

And then call it after the if-else statement:

if(playerScore < 5 || computerScore < 5) {
    if(playRound(playerSelection, computerSelection) == 'You win! Paper beats Rock' || playRound(playerSelection, computerSelection) == 'You win! Rock beats Scissors' || playRound(playerSelection, computerSelection) == 'You win! Scissors beats Paper') {
        playerScore += 1 // removed the return statement here
    } else if(playRound(playerSelection, computerSelection) == 'You lose! Paper beats Rock' || playRound(playerSelection, computerSelection) == 'You lose! Rock beats Scissors' || playRound(playerSelection, computerSelection) == 'You lose! Scissors beats Paper') {
        computerScore += 1 // removed the return statement here
    }
    return updateScoreboard(computerScore, playerScore);
} else if(playerScore == 5) {
    console.log('Congratulations!');
} else if(computerScore == 5) {
    console.log('You lose!');
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!