Trouble with Rock Paper Scissors game

Hi all,

I took a crack at the Rock Paper Scissors game in the Functions JS module. The code looks mostly correct, however the computer auto wins every single time despite me technically winning the game.

My code looks like this:

const getUserChoice = userInput =>{

userInput = userInput.toLowerCase();

if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’ || userInput === ‘bomb’){ return userInput}

else {console.log(“Error”)}

};

console.log(getUserChoice(‘Jim’))

const getComputerChoice = () => {

const randomNumber = Math.floor (Math.random() * 3)

switch (randomNumber){

case 0:

  return 'rock';

case 1: 

  return 'paper';

case 2:

  return 'scissors'

}

};

console.log(getComputerChoice())

const determineWinner = (userChoice, computerChoice) => {

if (userChoice === ‘bomb’)

{return ‘You won!’}

if (userChoice === computerChoice){

return 'The game was a tie!'}

if (userChoice === ‘rock’){

if (computerChoice === 'paper'){

  return 'The computer won!'}

else {return 'You won!'}

if (userChoice === ‘paper’){

if (computerChoice === 'scissors'){

  return 'The computer won!'}

  else {return 'You won!'}

if (userChoice === ‘scissors’) {

if (computerChoice === 'rock'){

  return 'The computer won!'}

  else {return 'You won!'}

}

}

}

}

console.log(determineWinner(‘rock’,‘paper’))

const playGame = () => {

const userChoice = getUserChoice(‘paper’)

const computerChoice = getComputerChoice()

console.log('You threw ’ + userChoice)

console.log('The computer threw ’ + computerChoice)

console.log(determineWinner(userChoice,computerChoice))

};

playGame();

The results look like this:

Error
undefined
rock
The computer won!
You threw paper
The computer threw rock
undefined

If someone could help me with this that would be great. Thanks!

Shreyas

Hello, and welcome back to the forums!

The determineWinner() function needs a review. The way it is structured now, if the the user’s choice isn’t bomb, rock, or if it doesn’t match the computer’s choice, then it will not be able to determine the winner.

It isn’t very noticeable because of the formatting, though. Here is your function with large indents and different lines for the curly braces to make it easier

const determineWinner = (userChoice, computerChoice) => {

    if(userChoice === 'bomb') {
        return 'You won!'
    }
  
    if(userChoice === computerChoice) {
        return 'The game was a tie!'
    }
  
    if(userChoice === 'rock') {
 
        if(computerChoice === 'paper') {
            return 'The computer won!'
        } else {
            return 'You won!'
        }
      
        if(userChoice === 'paper') {
            if(computerChoice === 'scissors') {
                return 'The computer won!'
            } else {
                return 'You won!'
            }
          
            if(userChoice === 'scissors') {

                if(computerChoice === 'rock') {
                    return 'The computer won!'
                } else {
                    return 'You won!'
                }
              
            }

        }

    }
}

The logic for the user choice being ‘paper’ is nested inside the code for ‘rock’, and the logic for the user choice being ‘scissors’ is nested inside ‘paper’. Neither can be reached because to get there, the user choice had to be ‘rock’ already. When the function doesn’t return something on it’s own, then you get the undefined value for your console.log().