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

173
Visualizações
Making "Rounds" in a Rock-Paper-Scissors Game?

I know this is a very popular/simple game to make, but I'm having a bit of trouble. I have made a rock-paper-scissors game in javascript and I've managed to make it work so that it will prompt the player to choose rock, paper, or scissors, get the computer to randomly pick an option, and a means for it to say "you lose" or "you win". The problem is that I'm required to make 5 rounds. That's the issue I'm having. I, for some reason, cannot seem to get the code to prompt the user 5 times. It only prompts the user once and runs the same code each time. My code is as follows:

     function playRound(playerSelection) {
    let computerSelection = getComputerSelection();
    if (playerSelection == "rock") {
        addToList(playerSelection, computerSelection);
        if (computerSelection == "rock") {
            result = "It's a tie";
        }
        else if (computerSelection == "paper") {
            result = "You lost! Paper beats Rock";
            computerScore++;
        }
        else {
            result = "You won! Rock beats Scissors";
            playerScore++;
        }
    }
    else if (playerSelection == "paper") {
        addToList(playerSelection, computerSelection);
        if (computerSelection == "rock") {
            result = "You won! Paper beats Rock";
            playerScore++;
        }
        else if (computerSelection == "paper") {
            result = "It's a tie";
        }
        else {
            result = "You lost! Scissors beats Paper";
            computerScore++;
        }
    }
    else {
        addToList(playerSelection, computerSelection);
        if (computerSelection == "rock") {
            result = "You lost! Rock beats Scissors";
            computerScore++;
        }
        else if (computerSelection == "paper") {
            result = "You won! Scissors beats Rock";
            playerScore++;
        }
        else
        result = "It's a tie";
    }
    h1.textContent = result;
    player.textContent = playerScore;
    computer.textContent = computerScore;
    game();
}

function game() {
    if (playerScore === 5) {
        end.style.display = "block";
        message.textContent = "You Won!";
    }
    else if (computerScore === 5) {
        end.style.display = "block";
        message.textContent = "You Lost!";
    }
}

I would appreciate any help or direction that is given. I have tried putting my "playRound" function outside of the loop, but it gives me the same issue. What would make it prompt twice?

Thank you!

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

0

I'm no expert in JS, so please correct me if i'm wrong, but what seems to be the case is that this variable:

const playerSelection = window.prompt("Enter your choice: ").toLowerCase();

is only initialized a single time with that function's return. (because it is outside any function) Accessing the variable only recalls it's value it was initialized with, causing the main loop to run over and over again with only that initial value.

replacing that line with someing like:

function getPlayerSelection(){
    return window.prompt("Enter your choice:").toLowerCase()
}

and replacing everywhere you use playerSelection with getPlayerSelection() should fix this issue.

about 4 years ago · Juan Pablo Isaza Relatório

0

That's because you are asking for player's prompt only once i.e. it is outside the playRound method. Try moving the following snippet into playround method.

const computerSelection = computerPlay();

const playerSelection = window.prompt("Enter your choice: ").toLowerCase();

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