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

214
Visualizações
Can't seem to figure out how to keep score in Rock, Paper, Scissors

I am trying to make Rock, Paper, Scissors via The Odin Project, and I'm stuck in the last step. I don't seem to have trouble getting the prompt to work, just can't seem to either tally up points, or I'm having trouble ending the game when either the player or computer gets to 5 points. I did include some console.logs at the bottom, but I'm unable to actually see the score due to the endless prompts. What am I doing wrong? I just need the game to end when either the player or computer gets to 5 points.

let playerScore = 0;
let computerScore = 0;

function computerPlay() {
    let allChoices = ["Rock", "Paper", "Scissors"];
    let randomChoice = allChoices[Math.floor(Math.random() * allChoices.length)];
    return randomChoice
}

function game() {
    while (playerScore <= 5 || computerScore <= 5) {
        const playerSelection = prompt("Would you like to choose R, P or S?")
        const computerSelection = computerPlay();
        alert(playRound(playerSelection, computerSelection));
    }
}

function playRound(playerSelection, computerSelection) {
    if (playerSelection == computerSelection) {
        return "Tie game!"
    } else if (playerSelection == "Rock" && computerSelection == "Scissors") {
        return `You win! ${playerSelection} beats ${computerSelection}!`
        playerScore += 1;
    } else if (playerSelection == "Paper" && computerSelection == "Rock") {
        return `You win! ${playerSelection} beats ${computerSelection}!`
        playerScore += 1;
    } else if (playerSelection == "Scissors" && computerSelection == "Paper") {
        return `You win! ${playerSelection} beats ${computerSelection}!`
        playerScore += 1;
    } else {
        return `You lose! ${computerSelection} beats ${playerSelection}`
        computerScore += 1;
    }
}

function winGame() {
    if (playerScore == 5) {
        return "You win!"
    } else if (computerScore == 5) {
        return "You lose!"
    }
}

game();

console.log(playerScore);
console.log(computerScore);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

function game() {
    while (playerScore < 5 && computerScore < 5) {
        const playerSelection = prompt("Would you like to choose Rock, Paper or Scissors?")
        const computerSelection = computerPlay();
        alert(playRound(playerSelection, computerSelection));
    }
}

function playRound(playerSelection, computerSelection) {
    if (playerSelection === computerSelection) {
        return "Tie game!"
    } else if (playerSelection === "Rock" && computerSelection === "Scissors") {
        playerScore += 1;
        return `You win! ${playerSelection} beats ${computerSelection}!`
        
    } else if (playerSelection === "Paper" && computerSelection === "Rock") {
        playerScore += 1;
        return `You win! ${playerSelection} beats ${computerSelection}!`
        
    } else if (playerSelection === "Scissors" && computerSelection === "Paper") {
        playerScore += 1;
        return `You win! ${playerSelection} beats ${computerSelection}!`

    } else {
        computerScore += 1;
        return `You lose! ${computerSelection} beats ${playerSelection}`
    }
}

You returned from the function before incrementing the points. So they will never be incremented. Also (playerScore <= 5 || computerScore <= 5) means, that the game will go on until both - playerScore and computerScore are below 6. It has to stop when one of them reaches 5

about 4 years ago · Juan Pablo Isaza Relatório
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