Javascript - Rock, Paper, Scissors Game

Hi all, my code works but it does not generate the intended outcome.

For example, when the user throws scissors, and computer throws rock, it states that the user won although the code states otherwise.

I differentiated the ‘won’ in various scenarios using numbers ‘won1’, ‘won2’… to test it out and realised it only prints ‘won1’ even though the scenario is different (refer to code).

I’m not sure what went wrong, grateful if someone can shed some light to this.

Here’s my code for reference, thanks!

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') {
    return userInput;
} else {
  console.log('Error! Please type in only rock, paper or scissors.')
}
}

const getComputerChoice = () => {
  const 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 'The game is a tie!';
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper')
    return 'The computer has won.';
  } else {
    return 'You won1!';
  }

  if (userChoice === 'paper' && computerChoice === 'scissors') {
    return 'The computer has won.';
  } else {
    return 'You won2!';
  }

  if (userChoice === 'scissors' && computerChoice === 'rock') {
    return 'The computer has won.';
  } else {
    return 'You won3!';
  }

  if (userChoice === 'bomb') {
    return 'You WON!';
  }
}

const playGame = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log(`You threw: ${userChoice}, Computer threw: ${computerChoice}`);
  console.log(determineWinner(userChoice,computerChoice));
}

playGame();

some of the else statements you have are causing problems:

That first else should be elseif(userChoice === 'paper' && computerChoice === 'rock')
That second else should be elseif(userChoice === 'scissors' && computerChoice === 'paper')

An alternative to this is to use a similar code to what you had for the first one (where you had nested if-statements).
I think that’s the better way to do this.