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

138
Visualizações
javascript function returns the same value when its supposed to be random

when i call function game() it keeps returning the same value from computerSelection even when i have it set to return a random value. when i call computerPlay() by itself it will give random value each time its called but not when its used in the game() function under the variable computerSelection.

  function computerPlay() {
  const plays = ["rock", "paper", "scissors"];
  return plays[Math.floor(Math.random() * plays.length)];
};



function playRound(playerSelection, computerSelection) {
  if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'paper') {
    console.log('paper beat rock')
    return ('paper beats rock')
  } else if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'rock') {
    console.log('its a tie, try again!')
    return ('its a tie, try again!')
  } else if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'scissors') {
    console.log('rock beats scissors')
    return ('rock beats scissors')
  } else {
    console.log('wut')
  }
};

function game() {
      playRound(playerSelection, computerSelection);
      playRound(playerSelection, computerSelection);
};



const playerSelection = "Rock";
let computerSelection = computerPlay();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should call computerPlay() once for each playRound(). So try this:

function game() {
      playRound(playerSelection, computerPlay());
      playRound(playerSelection, computerPlay());
};
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