Getting tie always with Rock Paper Scissors game Javascript

Viewed 41

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

const getcomputerChoice = () => {
  const 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 'Tie'
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'computer won'
    } else {
      return 'user won'
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'computer won'
    } else {
      return 'user won'
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'computer won'
    } else {
      return 'user won'
    }
  }
}

const playGame = () => {
  var userChoice = getUserChoice()
  console.log(userChoice)
  var computerChoice = getcomputerChoice()
  console.log(computerChoice)
  console.log(determineWinner(userChoice, computerChoice))
}

console.log(determineWinner())

So I wanted to do a project with JS, was following codeacadem's course on the subject. But for some reason I am always getting a tie which I do not understand why. Can anyone please help me? I thought this is the correct code and every single object was working properly.

UPDATE:

After giving a parameter to the userchoice (getUserChoice('scissors')) I got this error: userInput = userInput.toLowerCase();

TypeError: Cannot read properties of undefined (reading 'toLowerCase')

Initializing playgame didnt solve the issue

1 Answers

I've corrected your code with a few things:

  • As @James has commented, getUserChoice() expects a parameter for the choice of the user but none is given at playGame().
  • As @Alon Eitan has commented, console.log(determineWinner()) doesn't play the game at all as it just calls determineWinner() with the two parameters undefined. And actually that's why Tie is always returned as undefined == undefined.

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



const getcomputerChoice = () => {
  const 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 'Tie'
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'computer won'
    } else {
      return 'user won'
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'computer won'
    } else {
      return 'user won'
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'computer won'
    } else {
      return 'user won'
    }
  }
}

const playGame = () => {
  var userChoice = getUserChoice("rock")
  console.log(userChoice)
  var computerChoice = getcomputerChoice()
  console.log(computerChoice)
  console.log(determineWinner(userChoice, computerChoice))
}

playGame()

Related