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

308
Visualizações
The variables inside the functions can't be called

I'm doing this rock, paper, and scissors game, however, my last function it's not working properly, it won't execute that block of code, and what it's wrong with the variables in the last function, they cannot be called, I've tried to change the parameters, I also debugged the code and to the extent I notice just the last function it's not working properly

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput == "rock" || userInput == "paper" || userInput == "scissors") {
    return userInput;
  } else {
    console.log('Enter a valid option')
  };
};

function getComputerChoice(any) {
  return Math.floor(Math.random() * any)
}
switch (getComputerChoice(3)) //userInput
{
  case 0:
    console.log('rock');
    break;
  case 1:
    console.log('paper');
    break;
  case 2:
    console.log('scissors');
    break;
  default:
    console.log('Select a valid option')
}

function determineWinner(getUserChoice, getComputerChoice) {
  if (getUserChoice === getComputerChoice) {
    return 'The game was a tie'
  }
  if (getUserChoice === 'rock') {
    if (getComputerChoice === 'paper') {
      return 'The computer won!'
    } else {
      return 'You won'
    }
  }
  if (getUserChoice === 'scissors') {
    if (getComputerChoice === 'rock') {
      return 'Computer won!'
    } else {
      return 'You won!'
    }
  }
}
const playGam = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}
playGam();

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

0

As posted, getComputerChoice just does random math and multiplies by a (sometimes missing) param. Consider changing to return a random choice from the set of ['rock', 'paper', 'scissors']...

function getComputerChoice() {
  const choices = ['rock', 'paper', 'scissors'];
  return choices[Math.floor(Math.random() * choices.length)]
}

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput == "rock" || userInput == "paper" || userInput == "scissors") {
    return userInput;
  } else {
    console.log('Enter a valid option')
  };
};

function getComputerChoice() {
  const choices = ['rock', 'paper', 'scissors'];
  return choices[Math.floor(Math.random() * choices.length)]
}

switch (getComputerChoice(3)) //userInput
{
  case 0:
    console.log('rock');
    break;
  case 1:
    console.log('paper');
    break;
  case 2:
    console.log('scissors');
    break;
  default:
    console.log('Select a valid option')
}

function determineWinner(getUserChoice, getComputerChoice) {
  if (getUserChoice === getComputerChoice) {
    return 'The game was a tie'
  }
  if (getUserChoice === 'rock') {
    if (getComputerChoice === 'paper') {
      return 'The computer won!'
    } else {
      return 'You won'
    }
  }
  if (getUserChoice === 'scissors') {
    if (getComputerChoice === 'rock') {
      return 'Computer won!'
    } else {
      return 'You won!'
    }
  }
}
const playGam = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}
playGam();

We could also improve the code in a few other ways...

// make this global
const choices = ['rock', 'paper', 'scissors'];
const beats = { rock: 'scissors', scissors: 'paper', paper: 'rock' };

// general purpose fn to validate
const validateChoice = choice => {
  return choices.includes(choice.toLowerCase());
};

// a tightly specified thing to determine winner
const determineWinner(userChoice, computerChoice) {
  if (computerChoice === userChoice) return 'tie';
  return beats[userChoice] === computerChoice ? 'user win' : 'computer win';
}
 
about 4 years ago · Juan Pablo Isaza Relatório

0

You're not setting a variable value with your swtich statement. You need to return what's the computer's choice, so I've put your switch statement inside the getComputerChoice function and then it returns the output and you can use it in your playGam function. I've updated that as well to display the winner in every outcome.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput == "rock" || userInput == "paper" || userInput == "scissors") {
    return userInput;
  } else {
    console.log('Enter a valid option')
  };
};

function getComputerChoice(any) {
  const randomNo = Math.floor(Math.random() * any)
  switch (randomNo) //userInput
  {
    case 0:
      return 'scissors';
      break;
    case 1:
      return 'paper';
      break;
    case 2:
      return 'rock';
      break;
    default:
      return 'Select a valid option';
  }
}


function determineWinner(getUserChoice, getComputerChoice) {
  if (getUserChoice === getComputerChoice) {
    return 'The game was a tie'
  }
  switch (getUserChoice) {
    case 'paper': 
      return getComputerChoice === 'scissors' ? 'The computer won!' : 'You won';
    case 'rock' :
      return getComputerChoice === 'scissors' ? 'The computer won!' : 'You won';
    case 'scissors' :
      return getComputerChoice === 'paper' ? 'You won' : 'The computer won!';
    default: 
      return 'Something went wrong'
  }
}
const playGam = () => {
  const userChoice = getUserChoice('paper');
  const computerChoice = getComputerChoice(3);
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}
playGam();

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