Rock, paper, scissors If statements

Hello, I’d like someone to explain to me why this code doesn’t work for every if/else condition?
I know how to do it with nested if/else statements, but I was trying like this, and cant figure it out why second and third log is showing that I’m the winner? Also rest seems to be working fine. Thanks

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return ‘Game was a tie!’;
}
if (userChoice === ‘rock’ && computerChoice === ‘paper’) {
return ‘computer won’;
}
else {
return ‘you won’;
}
if (userChoice === ‘paper’ && computerChoice === ‘scissors’) {
return ‘computer won’;
}
else {
return ‘you won’;
}
if (userChoice === ‘scissors’ && computerChoice === ‘rock’) {
return ‘computer won’;
}
else {
return ‘you won’;
}
};

console.log(determineWinner(‘rock’, ‘paper’)); log: computer won
console.log(determineWinner(‘paper’, ‘scissors’)); log: you won
console.log(determineWinner(‘scissors’, ‘rock’)); log: you won
console.log(determineWinner(‘rock’, ‘rock’)); log: Game was a tie!
console.log(determineWinner(‘paper’, ‘rock’)); log: you won
console.log(determineWinner(‘scissors’, ‘paper’)); log: you won
console.log(determineWinner(‘rock’, ‘scissors’)); log: you won

Hi!

Would love to help. Can you re-post your code and press the </> format button before pasting? It’ll make it much easier to troubleshoot.

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'Game was a tie!';
  }
  if (userChoice === 'rock' && computerChoice === 'paper') {
    return 'computer won';
  } 
  else {
    return 'you won';
  }
  if (userChoice === 'paper' && computerChoice === 'scissors') {
    return 'computer won';
  } 
  else {
    return 'you won';
  }
  if (userChoice === 'scissors' && computerChoice === 'rock') {
    return 'computer won';
  } 
  else {
    return 'you won';
  }
};
 console.log(determineWinner('rock', 'paper'));
 console.log(determineWinner('paper', 'scissors'));
 console.log(determineWinner('scissors', 'rock'));
 console.log(determineWinner('rock', 'rock'));
 console.log(determineWinner('paper', 'rock'));
 console.log(determineWinner('scissors', 'paper'));
 console.log(determineWinner('rock', 'scissors'));

Hope this works

It’s how you’re using your else statements.

To highlight this for yourself, comment out all the other logs to see how the code is interpreting your code. You can even throw some console.log statements at different points to check where the code is getting stuck.

2 Likes