Issue with my rock paper scissors project

Hi Guys,

I’m having a bit of an issue with my rock paper scissors project.

I got to the end of the project and the game runs but it doesn’t run correctly in every outcome. when I test run user scissors and computer rocks it comes back with ‘you win’ instead of ‘computer wins’ and I can’t find where I’m going wrong with it.

I did write the correct code from the walkthrough video to compare it to my function, but the only difference I can see is the IF statements nested within each other, whereas I haven’t done that.’

So is my error due to me not nesting another if the statement blew each option?

Sorry if this doesnt make sense my head is absolutely fried, any help is appreciated.

Cheers.

const getUserChoice = userInput => { userInput = userInput.toLowerCase(); if ( userInput === "rock" || userInput === "paper" || userInput === "scissors" || userInput === "bomb" ) { return userInput; } else { console.log("error"); } }; function getComputerChoice() { const randomNumber = Math.floor(Math.random() * 3); if (randomNumber === 0) { return "rock"; } else if (randomNumber === 1) { return "scissors"; } else (randomNumber === 2) return "paper"; } function determineWinner(userChoice, computerChoice) { if (userChoice === computerChoice) { return "Tie"; } if (userChoice === "rock" && computerChoice === "paper") { return 'computer Wins' } else { return "You Win" } if (userChoice === "paper" && computerChoice === "scissors") { return "computer Wins" } else { return "you win" } if (userChoice === "scissors" && computerChoice === "rock"){ return "Computer Wins" } else { return " you Win" } if (userChoice === "bomb") { return "You killed us all! - You Win!"; } } console.log(determineWinner('paper', 'scissors')); console.log(determineWinner('scissors', 'rock')); console.log(determineWinner('scissors', 'scissors')); /* function playGame() { var userChoice = getUserChoice("rock", "paper", "scissors", 'bomb'); var computerChoice = getComputerChoice("rock", "paper", "scissors"); console.log(getUserChoice("rock")); console.log(getComputerChoice()); console.log(determineWinner(userChoice, computerChoice)); } playGame(); */

Hi there,

So, you summed up your own answer. But to give you reason, when a statement is returned, the return ends the execution of a function.

function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return "Tie";
  }
  if (userChoice === "rock" && computerChoice === "paper") {
    return 'computer Wins';
  } else {
    return "You Win"; //this line here ends the function
  }
  if (userChoice === "paper" && computerChoice === "scissors") {
    return "computer Wins";
  } else {
    return "you win";
  }
  if (userChoice === "scissors" && computerChoice === "rock"){
    return "Computer Wins";
  } else {
    return  "you Win";
  }
  if (userChoice === "bomb") {
  return "You killed us all! - You Win!";
  }
}

So, when your code gets to the first “else” the function stops running. That’s why they’re all nested in with

else if

Hey, thanks for the response, that is clearer thank you.

just to confirm, so after every ‘if’ return it should be ‘else if’ instead of else. and else should only be in the last if statement?

1 Like

If there are more than two conditions, I believe it to be best practice, yes. Though, depending on the length–or situation, you may find it easier to use case switching. (If you’ve learned about it yet)

Just tried writing it with else if statements and switch, but i was still getting the same issue.

but its fine, im going to reset the module and see if i missed anything. Hopefully i can do it on the second run through

Thanks again!

Sorry, I didn’t explain very well now that I’m looking at it again.

  } if (userChoice === "rock" && computerChoice === "paper") {
    return 'computer Wins'
  } else {
    return "You Win"
  }

This is why it’s returning “You Win” even if the computer should win. After the “tie” condition, the very next condition it checks is if the user chooses “rock” and the computer chooses “paper” the else then makes all other options return “You Win”. If you wanted to use && you would need to make sure to make an else if… for every condition returning every possibility.

e.g.,

function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return "Tie";
  } else if (userChoice === "rock" && computerChoice === "paper") {
    return 'computer Wins'
  } else if (userChoice === "rock" && computerChoice === "scissors") {
    return "you win"
  } else if (userChoice === "scissors" && computerChoice === "rock"){
    return "Computer Wins"
  } else if (userChoice === "scissors" && computerChoice === "paper") {
    return  "you Win"
  } else if (userChoice === "paper" && computerChoice === "scissors") {
    return "Computer wins"
  } else if (userChoice === "paper" && computerChoice === "rock") {
    return "You win"
  } if (userChoice === "bomb") {
  return "You killed us all! - You Win!";
  }
}

Ok at make more sense! I’ll give that a go when i get back round to it.

Hi there,
I saw your thread and thought I’d try to provide a perspective. I looked at your if/else chain and converted it to switch/case. I’ve run it multiple times, and the computer always wins given your parameters. Maybe try randomizing the computer’s choice? I haven’t done the module yet, so I don’t know what content is in there.

const getUserChoice = userInput => { userInput = userInput.toLowerCase(); if ( userInput === "rock" || userInput === "paper" || userInput === "scissors" || userInput === "bomb" ) { return userInput; } else { console.log("error"); } }; function getComputerChoice() { const randomNumber = Math.floor(Math.random() * 3); if (randomNumber === 0) { return "rock"; } else if (randomNumber === 1) { return "scissors"; } else (randomNumber === 2) return "paper"; } function determineWinner(userChoice, computerChoice) { switch(userChoice, computerChoice){ case "Tie": return 'tie'; break; case 'rock','paper': return 'computer wins'; break; case 'paper','scissors': return 'computer wins'; break; case 'paper','scissors': return "computer wins"; break; case 'scissors','rock': return "computer wins"; break; case 'bomb': return 'You killed us all! - You Win!'; break; default: return 'Try Again!'; break; } } console.log(determineWinner('paper', 'scissors')); console.log(determineWinner('scissors', 'rock')); console.log(determineWinner('scissors', 'scissors')); function playGame() { var userChoice = getUserChoice("rock", "paper", "scissors", 'bomb'); var computerChoice = getComputerChoice("rock", "paper", "scissors"); console.log(getUserChoice("rock")); console.log(getComputerChoice()); console.log(determineWinner(userChoice, computerChoice)); } playGame();