How did i make it random whether the result says win, lose or tie? (rock paper scissors project with javascript)

I have been doing the javascript challenge for rock, paper, scissors, but somehow i have made it so that it is entirely random whether the program tells me the user won or if the computer won or if it’s a tie. what the actual result should be seems to have no bearing. (so even if it’s two papers, it’ll sometimes say computer wins)

console.log('hi');
const getUserChoice = userInput=> {
  userInput = userInput.toLowerCase()
  if (userInput === "rock" || userInput === "paper" || userInput ==="scissors") {
  return userInput;  
  } else {
      console.log("error!");
  };
} ;
//console.log(getUserChoice("Rock"))
const getComputerChoice = () => {
  choice = Math.floor(Math.random()*3);
  //console.log(choice)
  switch (choice) {
    case 0:
      return "rock";
      break;
    case 1:
      return "paper";
      break;
    default:
      return "scissors";
  };
};



const determineWinner = (userChoice, computerChoice) => {
  userChoice = getUserChoice("scissors");
  computerChoice = getComputerChoice();
  switch(true){
    case(userChoice === computerChoice):
      return "it's a tie!";
      break;
    case (userChoice === "scissors" && computerChoice == "rock"):
      return "computer wins!";
      break;
    case (userChoice === "rock" && computerChoice === "paper"): 
      return "computer wins!";
      break;
    case (userChoice == "paper" && computerChoice == "scissors"):
      return "computer wins!";
      break;
    default:
      return "user wins!";
  }

}
const playGame = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log("user picked",userChoice);
  console.log("computer picked", computerChoice);
  console.log(determineWinner(userChoice, computerChoice)) 
}
playGame()

could someone tell me what’s gone wrong here?

1 Like

I think the way you are using the switch statement may not be correct. It might be more appropriate to use an if/else statement instead.

Here’s why I think that. From how I understand switch statements, the expression is usually a combination of values, variables, and operators, which computes to a value. Case statements evaluate the expression and execute if the case matches the value of the expression. In your code you are using “true” as the expression, which looks like it might be causing some funky/buggy behavior. “true” is a boolean value and you don’t have any case statements for “true”.

Have you tried using if/else instead of a switch? If yes, I’d love to learn more about why you chose a switch vs if/else.

1 Like

My apologies, I should have been more specific. The switch statement in your determineWinner function is the one I am speaking about.

1 Like

Those two lines should not be inside the function. They are resetting the parameters. Suggest remove those lines.

There is nothing wrong with the switch. Both expressions are boolean, so will work as expected.

3 Likes

when i remove those lines, it gives me an error telling me neither of them are defined.

You only need to remove them from the determineWinner function. You can keep them in the playGame function.

As @mtf mentioned, basically you are providing parameters in the playGame function. But when you call the determineWinner function you are re-assigning those values instead of keeping the ones you passed in the playGame function.

const playGame = () => {
  const userChoice = getUserChoice('scissors'); <--- Here
  const computerChoice = getComputerChoice();<--- Here
  console.log("user picked",userChoice);
  console.log("computer picked", computerChoice);
  console.log(determineWinner(userChoice, computerChoice)) 
}
const determineWinner = (userChoice, computerChoice) => {
  userChoice = getUserChoice("scissors"); <---Reassigning the value
  computerChoice = getComputerChoice(); <---Reassigning the value
  switch(true){
    case(userChoice === computerChoice):
      return "it's a tie!";
      break;
    case (userChoice === "scissors" && computerChoice == "rock"):
      return "computer wins!";
      break;
    case (userChoice === "rock" && computerChoice === "paper"): 
      return "computer wins!";
      break;
    case (userChoice == "paper" && computerChoice == "scissors"):
      return "computer wins!";
      break;
    default:
      return "user wins!";
  }

}
2 Likes

it’s working now; thanks both!

2 Likes

And I learned something about switches! Thank you for that @thelegend2789 @mtf !

2 Likes