Rock Paper Scissors Project

My code always says it’s a “tie” whether it’s a tie or not. What am I doing wrong? Thanks.

// User input
const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  } else {
    console.log('Error!');
  }
};
console.log(getUserChoice('rock'));

// Computer choice
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;
    default:
      console.log('Error!');
      break; 
  }
}
console.log(getComputerChoice());

// Determine winner
const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
      return 'The game is a tie!';
    } 
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!';
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'The computer won!';
    } else {
      return 'You won!';
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!';
    }
  }
  }

console.log(determineWinner());

your function has two parameters:

const determineWinner = (userChoice, computerChoice)

but when you call/invoke the function you provide no arguments:

console.log(determineWinner());

I personally think JS should throw an error for this, anyway, different story

Because you provide no arguments, the parameters are undefined. Which will equal each other

Thanks for your quick response! Seems like a simple error that I overlooked. Thanks again.

Recommendation for next time, inspect the values to see what might be causing the issue, you could use .log() for example:

const determineWinner = (userChoice, computerChoice) => {
   console.log (userChoice, computerChoice)

Pardon my ignorance. Code newbie here. Looking for some assistance. Keep getting an error message saying, “ReferenceError: userChoice is not defined.” Been searching through JavaScript documentation and can’t seem to figure out how to fix it. Thanks.

// User input
const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  } else {
    console.log('Error!');
  }
};
console.log(getUserChoice('rock'));

// Computer choice
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;
    default:
      console.log('Error!');
      break; 
  }
}
console.log(getComputerChoice());

// Determine winner
const determineWinner = function(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
      return 'The game is a tie!';
    } 
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!';
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'The computer won!';
    } else {
      return 'You won!';
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!';
    }
  }
  }
console.log(determineWinner(userChoice,computerChoice));

here:

console.log(determineWinner(userChoice,computerChoice));

you use variables you haven’t defined anywhere, resulting an error. Where do you think these variables are defined?

Also, what are you trying to do here? You tried to determine who won a game of RPS. So shouldn’t you get the user and computer choice? You wrote functions for that :wink: