Why isn't this code working? Please help

Here is the following code I have done for a Codecademy course project:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();

  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } else {
    console.log('Error!');
  }
}

const getComputerChoice = () => {
 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 was a tie!';
  } else if(userChoice === 'rock' && computerChoice === 'paper') {
    return 'The computer won the game';
  } else {
    return 'You have won the game!';
  }
  
  if(userChoice === 'paper' && computerChoice === 'scissors') {
    return 'The computer won the game';
  } else {
    return 'You have won the game!';
  }
  
  if(userChoice === 'scissors' && computerChoice === 'rock') {
    return 'The computer has won the game';
  } else {
    return 'You have won the game!';
  }
}
console.log(determineWinner('paper', 'scissors'));
console.log(determineWinner('paper', 'paper'));
console.log(determineWinner('scissors', 'rock'));

There are strings logging to the console when I run the code, but just not the right ones.

What is it about my if statements in the determineWinner function would make the return not print? For example, when I run this the only block that calls correctly is “‘The game is a tie!’”. The other ones just log “‘You have won the game!’”. I don’t understand what I did wrong here.

Also, please excuse me if I I’m not explaining myself correctly with all of the correct terms regarding JS. I’m still learning, and welcome correction for any terms missused (i.e., call, log, function, etc)! :slight_smile:

const determineWinner = (userChoice, computerChoice) => {
  if(userChoice === computerChoice) {
    return 'The game was a tie!';
  } else if(userChoice === 'rock') {
    if (computerChoice === 'paper') {
    return 'The computer won the game';
  } else {
    return 'You have won!';
  } 
  }

Ah… hm, okay. So, then would this adjustment to the logic allow the other if blocks to run?

I just did! It worked :slight_smile: thank you for your help with the code and the note about forum post readability!

2 Likes