Rock Paper Scissors Returns Undefined

Here is my code:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  switch(userInput) {
    case 'rock':
      return userInput;
      break;
    case 'scissors':
      return userInput;
      break;
    case 'paper':
      return userInput;
      break;
    default:
      console.log('Error: Code 1')
      break;
  }
}
function getComputerChoice() {
  num = Math.floor(Math.random() * 3)
  switch(num) {
    case 0:
      return 'rock'
      break;
    case 1:
      return 'paper'
      break;
    case 2:
      return 'scissors'
      break;
    default:
      console.log('Error: Code 2')
      break;
  }
}
function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'Tie!'
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'Computer won!';
    } else {
      return 'User won!';
    };
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'Computer won!';
    } else {
      return 'User won!';
    };
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'Computer won!';
    } else {
      return 'User won!';
    };
  };
  };
  };
  };
};
console.log(determineWinner('paper', 'scissors'));

When I test my code in the console.log() line at the bottom, it just returns ‘undefined’.

1 Like

Welcome to the forums :slight_smile:

Missing a curly bracket here:

function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'Tie!'
  if 

The undefined means that your determineWinner function is getting stuck at some point so I’d check the syntax with little tracer console.logs at every juncture you need.

Additionally I’m not sure what this is for. Consider formatting it differently if it belongs to something as it’ll be hard to troubleshoot if you have to do anything relating these.

  };
  };
  };
  };
1 Like

Thanks for the advice! :smile:
Where am I missing a curly bracket?
Not sure what those “};” are but I get errors without them, so I guessed they’re supposed to be there.

Where am I missing a curly bracket?

I quoted the section

function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'Tie!'
  if 

If you don’t put it there the whole function will run only if this condition is true: (userChoice === computerChoice)

Not sure what those “};” are but I get errors without them, so I guessed they’re supposed to be there.

It’s fine that you did this but I wouldn’t put them without knowing where the match up. I’d play with indentation to make it clearer for myself. Particularly the larger the code you write gets, it gets dangerous to lose track of the curly brackets. :sweat_smile:

1 Like

Oh yeah, thanks. Read it wrong!
I think that’s the issue. :slight_smile:

For anyone else with this issue, I fixed it. This is my code now.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  switch(userInput) {
    case 'rock':
      return userInput;
      break;
    case 'scissors':
      return userInput;
      break;
    case 'paper':
      return userInput;
      break;
    default:
      console.log('Error: Code 1')
      break;
  }
}
function getComputerChoice() {
  num = Math.floor(Math.random() * 3)
  switch(num) {
    case 0:
      return 'rock'
      break;
    case 1:
      return 'paper'
      break;
    case 2:
      return 'scissors'
      break;
    default:
      console.log('Error: Code 2')
      break;
  }
}
function determineWinner(userChoice, computerChoice) {
  console.log('Function Started')
  if (userChoice === computerChoice) {
    console.log('Tie!')
    return 'Tie!'
  };
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      console.log('computer won')
      return 'Computer won!';
    } else {
      console.log('User won')
      return 'User won!';
    };
  };
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'Computer won!';
    } else {
      return 'User won!';
    };
  };
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'Computer won!';
    } else {
      return 'User won!';
    };
  };
  };
console.log(determineWinner('rock', 'scissors'));

I moved the brackets from the bottom to the correct place.