Issue with computer choice in rock paper scissors

I have a problem… When I run my code, computerChoice is undefined. It’s not wrong, it is. I know that my random number should be the value of computerChoice, but how am I meant to implement it into my code? Instead of computerChoice shouldn’t I just put randomNumber? e.g:
const determineWinner = (userChoice, randomNumber) => {
Here’s the full code:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput
  } else{
    console.log('ERROR!')
  }
};
const getComputerChoice = () => {
  randomNumber = Math.floor(Math.random()*3)
  switch(randomNumber) {
    case 0:
      return 'rock';
      break;
    case 1:
      return 'paper';
      break;
    case 2:
    return 'scissors';
    break;
  }
}
const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'Tie!';
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'The user won!';
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'rock') {
      return 'The user won!';
    } else {
      return 'The computer won!'
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'paper') {
      return 'The user won!';
    } else {
      return 'The computer won!';
    }
  }
}

const playGame = () => {
  console.log(determineWinner(2, computerChoice));

}
playGame();

you have a function to get a random computer choice (getComputerChoice), why not use it?

But how? By replacing computerChoice with getComputerChoice so that whatever returns is one of the arguments?

Sounds like a great idea :slight_smile:

or if you really want computerChoice variable, you could call the getComputerChoice function, store the result in computerChoice variable, and then pass computerChoice variable to determineWinner function. But that is just an extra step

what you suggested (calling getComputerChoice as one of determineWinners arguments) is a better idea

Thanks for the help! Got it :grin:

1 Like