Rock, paper, scissors issue : "The game is a tie"

Hi all.

I’m stuck with the project Rock, Paper, Scissors

The code stops at the comparison between the userChoice and the computerChoice. I cannot see what I have done wrong.


const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock') {
    console.log('Rock on.')
    return userInput;
  } else if (userInput === 'paper') {
    console.log('Papers please.')
    return userInput;
  } else if (userInput === 'scissors') {
    console.log('Scissor me timbers.')
    return userInput;
  } else {
    console.log('Wrong hand! Try rock, paper or scissors.')
  }
}

const getComputerChoice = () => {
  let randNum = Math.floor(Math.random() *3);
  if (randNum === 0) {
     console.log('rock')
    return 'rock';
  } else if (randNum === 1) {
     console.log('paper')
    return 'paper';
  } else if (randNum === 2) {
    console.log('scissors')
    return 'scissors';
  }
}

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    console.log('This game was a tie.')
    return 'This game was a tie.'
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      console.log('Computer wins with paper.')
      return 'Computer wins with paper.'
    } else {
      console.log('Player wins with paper.')
      return 'Player has won with rock.'
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      console.log('Computer wins with scissors.')
      return 'Computer wins with scissors'
    } else {
      console.log('Player wins with paper.')
      return 'Player wins with paper.'
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      console.log('Computer wins with rock.')
      return 'Computer wins with rock.'
    } else {
      console.log('Player wins with scissors.')
      return 'Player wins with scissors'
    }
  }
}

determineWinner()

const playGame = () => {
  let userChoice = getUserChoice('rock')
  console.log(userChoice)
  let computerChoice = getComputerChoice()
  console.log(computerChoice)
  console.log(determineWinner(userChoice, computerChoice))
}

Hi,
that’s because you call the function determineWinner() rather than playGame(). playGame() never runs.

1 Like

Thanks. I removed the call to determineWinner. But now I just get “undefined”, when I try the code in the console in Chrome browser.

Did you call playGame() instead?

1 Like

Thanks for giving me extra special attention :smiley: It works now :slight_smile:

1 Like