Cannot fix this project

I cannot get the bomb function of my rock, paper, scissors to work? when I set the user’s choice to bomb (or any choice) it will say it’s a tie, when I remove the ‘bomb’ logic from determineWinner it works fine…

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('invalid choice!')
  }
};

const getComputerChoice = () => {
  
  switch (Math.floor(Math.random() * 3)) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
  }
};


const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === 'bomb') {
    return 'You blew your opponent up!'
  }
  if (userChoice === computerChoice) {
    return 'It\'s a tie!';
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'Computer wins!';
    } else {
      return 'You win!';
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'Computer wins!';
    } else {
      return 'You win!';
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'Computer wins!';
    } else {
      return 'You win!';
    }
  }

};

const playGame = () => {
  const userChoice = getUserChoice('paper');
  const computerChoice = getComputerChoice();
  
  console.log('You played: ' + userChoice);
  console.log('The computer played: ' + computerChoice);
  
  console.log(determineWinner());
}

playGame()

inserting a log statement within your function clarifies a lot:

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

undefined will always equals undefined.

i took this step (inserting log statement) because i wanted to see the values you use for comparison, which helps determining what goes wrong.

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.