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

137
Visualizações
How do I prompt user for input after each round in rock paper scissors?

The game works fine after 1 round. I'm trying to play 5 rounds. I tried a for-loop before the while loop. But, it still only prompts the user for input one time.

let computerPlay = () => {
      let compChoices = ["rock", "paper", "scissors"]; //array storing computer possible actions
      let i = Math.floor(Math.random() * 3);
      let choice = compChoices[i];
    
      return choice;
    };
    
    //
    let playerChoice = prompt("Select your action").toLowerCase();
    let computerChoice = computerPlay();
    
    let playRound = (playerSelection, computerSelection) => {
      let winner = "";
      if (playerSelection === computerSelection) {
        winner = "tie";
      } else if (playerSelection > computerSelection) {
        winner = "player";
      } else {
        winner = "computer";
      }
    
      return console.log(winner);
    };
    
    let i = 0;
    while (i < 5) {
      playRound(playerChoice, computerChoice);
      i++;
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

UPDATED

As requirement, author is asking to keep playerSelection and computerSelection params for playRound. I'd propose the fix is to put playerChoice and computerChoice in while loop

let computerPlay = () => {
      let compChoices = ["rock", "paper", "scissors"]; //array storing computer possible actions
      let i = Math.floor(Math.random() * 3);
      let choice = compChoices[i];
    
      return choice;
    };
    
    let playRound = (playerSelection, computerSelection) => {
      let winner = "";
      if (playerSelection === computerSelection) {
        winner = "tie";
      } else if (playerSelection > computerSelection) {
        winner = "player";
      } else {
        winner = "computer";
      }
    
      return console.log(winner);
    };
    
    let i = 0;
    while (i < 5) {
      //here is the change
      let playerChoice = prompt("Select your action").toLowerCase();
      let computerChoice = computerPlay();
      playRound(playerChoice, computerChoice);
      i++;
    }

OLD ANSWER

Your problem is you pass prompt outside of your playRound. playerSelection and computerSelection should be in playRound too that would help for resetting values of computer and your choices' every round.

let computerPlay = () => {
      let compChoices = ["rock", "paper", "scissors"]; //array storing computer possible actions
      let i = Math.floor(Math.random() * 3);
      let choice = compChoices[i];
    
      return choice;
    };
    
    let playRound = () => {
      let playerSelection = prompt("Select your action").toLowerCase();
      let computerSelection = computerPlay();
      let winner = "";
      if (playerSelection === computerSelection) {
        winner = "tie";
      } else if (playerSelection > computerSelection) {
        winner = "player";
      } else {
        winner = "computer";
      }
    
      return console.log(winner);
    };
    
    let i = 0;
    while (i < 5) {
      playRound();
      i++;
    }
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