Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
How do I stop counting scores once a player has reached the max score?

Working on The Odin Project Rock-Paper-Scissors enhancement considering DOM manipulation. Been doing it for 3 days, re-worked it a few times, (current version was done via tutorial) and managed to stop the counter once a score of 5 is reached. But, both the computer and player side can still get to 5 points via clicking, even if the opponent has already reached 5pts.

How can I stop the counter for a player if the opponent reached the goal first?

Here's the code:

const SELECTIONS = [
    {
        name: "rock",
        beats: "scissors"
    },
    {
        name: "paper",
        beats: "rock"
    },
    {
        name: "scissors",
        beats: "paper"
    }
]
const yourScoreSpan = document.querySelector("[data-your-score]");
const computerScoreSpan = document.querySelector("[data-computer-score]");

//Player plays
choices.forEach(choice => {
    choice.addEventListener("click", e => {
        const selectionName = choice.dataset.choice;
        const selection = SELECTIONS.find(selection => selection.name === selectionName)
    
        makeSelection(selection)
    })
})


//Selecting R,P,S
function makeSelection(selection) {
    const computerSelection = randomSelection();
    const yourWinner = isWinner(selection, computerSelection);
    const computerWinner = isWinner(computerSelection, selection);
    

    
    if((yourWinner && (yourScoreSpan.innerText < 5) && (computerScoreSpan.innerText !== 5))) 
    incrementScore(yourScoreSpan);
    

    if(computerWinner && (computerScoreSpan.innerText < 5) && (yourScoreSpan.innerText !== 5)) incrementScore(computerScoreSpan);
}

//Incrementing Scores
function incrementScore(scoreSpan) {
    scoreSpan.innerText = parseInt(scoreSpan.innerText) + 1
}


//Computer plays
function randomSelection() {
    const randomIndex = Math.floor(Math.random() * SELECTIONS.length);
    return SELECTIONS[randomIndex];
}

//Who wins
function isWinner(selection, opponentSelection) {
    return selection.beats === opponentSelection.name
}

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda