Rock, paper, scissors debug help

Hello everyone!
this is giving me a little bit of a headache!
the game works except with this condition.
the computer should win, but I get the "undefined’ response.
I’m also going to link the script. Thanks everyone! :upside_down_face:

const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
  if(userInput === 'rock'|| userInput === 'paper'|| userInput === 'scissors' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('errore, non hai inserito un sasso carta o forbice!');
  }
}

function 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 === 'bomb') {
  return 'vincerai per sempre!';
}
  if (userChoice === computerChoice) {
    return 'hai pareggiato';
  } 
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'Il computer ha vinto';
    } else {
      return 'hai vinto tu!';
    } 
 if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'Il computer ha vinto';
    } else {
      return 'hai vinto tu!';
    } 
 if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'Il computer ha vinto';
    } else {
      return 'hai vinto tu!';
    } 
    }
    }
  }
};
const playGame = () => {
const userChoice = getUserChoice('paper');
const computerChoice = getComputerChoice();
console.log('you threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
};
playGame();




1 Like

Hey @lunadore.lc! Welcome to the community.

For what I see, I think you are nesting your if conditions for comparing the value of userChoice inside the if(userChoice === ’rock’) condition.

Let me see if I can better explain what I mean.

First, you check to see if the userChoice was ‘bomb’. If it is, you print ‘vincerai per sempre’. So far so good.

Then, you check if the game was a tie. If it is, you print ‘hai pareggiato’. So far so good.

Then, you check if the userChoice was ‘rock’. If it is, you then check if either the user or the computer won. So far so good.

But then, you forget to close that last if statement with a }, therefore every other condition where you compare the value of userChoice gets nested inside that if. That means that the program is really only checking if the userChoice was ‘rock’. And since the other conditions are nested in that one, the program will only check if the userChoice is something else than ‘rock’ IF the userChoice IS rock. Which I think, doesn’t really make sense.

And therefore, the determineWinner function will not know what to return unless userChoice is ‘rock’.