Help with rock paper scissors

I keep getting undefined where its supposed to say you win or lose on the console. I’m having difficulty understanding where i went wrong.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') {
  return userInput;
} else {
  console.log('Error! Please type rock, paper, or scissors!');
};
}
const getComputerChoice = () => {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
  case 0:
    return 'rock';
  case 1:
    return 'paper';
  case 2:
    return 'scissors';
     }
}

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
  return 'The game is a tie!';
}
  if (userChoice === 'rock') {
  if (computerChoice === 'paper') {
    return 'You lost to the Computer!';
  } else {
    return 'You won!';
  }    
}
  if (userChoice === 'paper') {
  if (computerChoice === 'scissors') {
    return 'You lost to the Computer!';
  } else {
    return 'You won!';
  }
    if (userChoice === 'scissors') {
  if (computerChoice === 'rock') {
    return 'You lost to the Computer!';
  } else {
    return 'You won!';
  }
}
    if (userChoice === 'bomb') {
      return 'You Win, Cheater!';
    }
  }  
}

const playGame = () => {
  const userChoice = getUserChoice('bomb');
  const computerChoice = getComputerChoice();
  console.log('I choose you: ' + userChoice);
  console.log('The Computer threw: ' + computerChoice);
  
  console.log(determineWinner(userChoice, computerChoice))
};


playGame();

Hello, @codemaster05311.

Welcome to the forums.

Let’s take a close look at your determineWinner function. I cleaned up the indentation:

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
  return 'The game is a tie!';
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'You lost to the Computer!';
    } else {
      return 'You won!';
    }    
  }
  if (userChoice === 'paper') { //where is the closing brace for this block?
    if (computerChoice === 'scissors') {
      return 'You lost to the Computer!';
    } else {
      return 'You won!';
    }
    if (userChoice === 'scissors') {
      if (computerChoice === 'rock') {
        return 'You lost to the Computer!';
      } else {
        return 'You won!';
      }
    }
    if (userChoice === 'bomb') {
      return 'You Win, Cheater!';
    }
  } //found it! 
}

If the userChoice is ‘rock’, you’ve handled things nicely. If the userChoice is ‘paper’, you’ll get an actual winner returned. However, if the userChoice is not rock or paper what happens? I didn’t alter your code other than indentation, and added a couple of comments. With code properly indented it is much easier to follow the logic.

Thanks Tod! I was going crazy.:joy::joy::joy:

1 Like