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

128
Visualizações
Rock Paper Scissors game returns undefined along with expected result

When running the function the console logs the expected result followed by "Undefined" directly below it and I can't figure out why. I am just starting to learn javascript so I am sure this is going to be an easy solve but I'm just not seeing it at the moment.

  userInput = userInput.toLowerCase();
  if(userInput !== 'rock' && userInput !== 'paper' && userInput !== 'scissors') console.log('Inocrrect choice, please try again');
  return userInput;
}

const getComputerChoice = () => {
  let computerChoice = Math.floor(Math.random()*3);
  switch(computerChoice){
    case 0:
      return 'rock';
      break;
    case 1:
      return 'paper';
      break;
    case 2:
      return 'scissors';
      break; 
  }
}

const determineWinner = (userChoice, computerChoice) => {
  if(userChoice === computerChoice) {
    console.log(`You chose ${userChoice} and the computer chose ${computerChoice}, you tied!`);
  } else if(userChoice === 'rock'){
      if(computerChoice === 'scissors'){
        console.log(`You chose ${userChoice} and the computer chose ${computerChoice}, you won!`);
      } else {
        console.log(`You chose ${userChoice} and the computer chose ${computerChoice}, you lost!`);
      }
    }
    else if(userChoice === 'paper'){
      if(computerChoice === 'rock'){
        console.log(`You chose ${userChoice} and the computer chose ${computerChoice}, you won!`);
      } else {
        console.log(`You chose ${userChoice} and the computer chose ${computerChoice}, you lost!`);
      }
    }
    else if(userChoice === 'scissors'){
      if(computerChoice === 'paper'){
        console.log(`You chose ${userChoice} and the computer chose ${computerChoice}, you won!`);
      } else {
        console.log(`You chose ${userChoice} and the computer chose ${computerChoice}, you lost!`);
      }
    }
}

const playGame = userInput => {
  const userChoice = getUserChoice(userInput);
  const computerChoice = getComputerChoice();
  console.log(determineWinner(userChoice,computerChoice));
}

playGame('rock');
about 4 years ago · Juan Pablo Isaza
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