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

135
Visualizações
Rock Paper Scissors game only one outcome unless I refresh page

So im making The Odin Project's rock paper scissors game and the only problem i have is that the computer's selection is always the same unless I refresh the page.

for example if i choose Rock three times in a row, the computer's selection will always be the same unless i refresh the page.

const thing1 = "Rock",
    thing2 = "Paper",
    thing3 = "Scissors";

function computerPlay() {
    let a = Math.random();
    if (a <= 0.33) {
        return thing1;
    } else if (a > 0.33 && a < 0.67) {
        return thing2;
    } else {
        return thing3;
    }
}

let playerScore = 0,
    computerScore = 0;

const playerSelection = ["Rock", "Paper", "Scissors"];
let computerSelection = computerPlay();

function playRound(playerSelection, computerSelection) {
    if (playerSelection.toLowerCase() == "rock" && computerSelection == thing2) {
        
        computerScore++;
        return "You lose! Rock loses to Paper";
    } else if (playerSelection.toLowerCase() == "rock" && computerSelection == thing3) {
        
        playerScore++;
        return "You win! Rock beats Scissors";
    } else if (playerSelection.toLowerCase() == "paper" && computerSelection == thing1) {
        playerScore++;
        return win = "You win! Paper beats Rock";
    } else if (playerSelection.toLowerCase() == "paper" && computerSelection == thing3) {
        computerScore++;
        return win = "You lose! Paper loses to Scissors";
    } else if (playerSelection.toLowerCase() == "rock" && computerSelection == thing1) {
        computerScore++;
        return win = "You lose! Rock loses Scissors";
    } else if (playerSelection.toLowerCase() == "scissors" && computerSelection == thing2) {
        playerScore++;
        return win = "You win! Scissors beats Paper!";
    } else {
        return "Tie!";
    }
}



function game() {
    let playerSelection = prompt("Rock, paper or scissors?");
    result = playRound(playerSelection, computerSelection);
    scoreboard = "User:" + " " + playerScore + " " + "Computer" + " " + computerScore;
    console.log(scoreboard);
    return result;
    

}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You only call computerPlay() once, leading it computerSelection being the exact same every round. Call it every round to get a new random value.

function game() {
    let playerSelection = prompt("Rock, paper or scissors?");
    computerSelection = computerPlay(); // <---
    result = playRound(playerSelection, computerSelection);
    scoreboard = "User:" + " " + playerScore + " " + "Computer" + " " + computerScore;
    console.log(scoreboard);
    return result;
}
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