Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda