Javascript - Rock, paper, scissors project - help with if statements, AND OR

Hello,

I’ve just completed the rock, paper, scissors project and was looking back over the code.

Can someone help me and explain why this wouldn’t work and if there’s a way to make it work without writing out all the separate if statements.

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return "The game was a tie!";
  }
  if ((userChoice === "rock" && computerChoice === "paper") || (userChoice === "scissors" && computerChoice === "rock") || (userChoice === "paper" && computerChoice === "scissors")) {
    return "The computer won!";
  } else {
    return "You won!";
  }

Thank you :slight_smile:

Hi,
your conditions are perfectly fine. My assumption is that your ‘determineWinner’ function does not receive valid arguments for userChoice and computerChoice.
What happens if you add a console and log the parameters?

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

@mirja_t Thank you for your quick reply :slight_smile:

I went back to my code to paste the whole thing and I just noticed I’d commented out the last curly bracket I needed:

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

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

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return "The game was a tie!";
  }
  if ((userChoice === "rock" && computerChoice === "paper") || (userChoice === "scissors" && computerChoice === "rock") || (userChoice === "paper" && computerChoice === "scissors")) {
    return "The computer won!";
  } else {
    return "You won!";
  }

  /*if (userChoice === "rock" && computerChoice === "paper") {
    return "The computer won!";
  } else {
    return "You won!";
  }
  if (userChoice === "scissors" && computerChoice === "rock") {
    return "The computer won!";
  } else {
    return "You won!";
  }
  if (userChoice === "paper" && computerChoice === "scissors") {
    return "The computer won!";
  } else {
    return "You won!";
  }
  if (userChoice === "bomb") {
    return "You won!";
  }
};*/

const playGame = () => {
  const userChoice = getUserChoice("bomb");
  const computerChoice = getComputerChoice();
  console.log(`You threw: ${userChoice}`);
  console.log(`The computer threw: ${computerChoice}`);
  console.log(determineWinner(userChoice, computerChoice));
};

playGame();

It’s obvious now why I was getting a syntax error :woman_facepalming:
It all works as it’s supposed to now.

1 Like