Home > front end >  How to Freeze a Counter when a Condition is Met
How to Freeze a Counter when a Condition is Met

Time:04-27

I am creating a rock paper scissors game, and I want the counter to freeze at its current state when either the player or the computer reaches five first. So far, the counter will continue to go up when the buttons are clicked, even though I added a gameOver function. I didn't want to just add a modal because I want to see how this would be solved. Any tips?

'use strict';
//DOM Elements
const rockDiv = document.querySelector('#rock');
const paperDiv = document.querySelector('#paper');
const scissorsDiv = document.querySelector('#scissors');

const userScore = document.getElementById('userScore');
const cpuScore = document.getElementById('compScore');

const restartBtn = document.getElementById('restartBtn');

//Starting Conditions

const ROCK = 'rock';
const PAPER = 'paper';
const SCISSORS = 'scissors';
const options = [ROCK, PAPER, SCISSORS];
let playerScore = 0;
let computerScore = 0;

//Will update Result Message
const displayMessage = function (message) {
  document.querySelector('.results').textContent = message;
};
//Computer Choice
const computerPlay = function () {
  return options[Math.floor(Math.random() * options.length)];
};

const playRound = function (playerSelection, computerSelection) {
  //Use an if/else statement
  if (
    (playerSelection === 'rock' && computerSelection === 'scissors') ||
    (playerSelection === 'paper' && computerSelection === 'scissors') ||
    (playerSelection === 'scissors' && computerSelection === 'paper')
  ) {
    playerScore  ;
    userScore.innerHTML = playerScore;
    displayMessage(`You won            
  • Related