Having trouble with rock paper scissors and userInput.toLowerCase

Hi all, I’m working through the rock paper scissors project and keep getting the same error. I have looked at other forum posts with similar issues but I just can’t wrap my head around what I’m getting wrong.
Many thanks in advance for any help!
here’s my code;

onst 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 'scissors';
  case 2:
  return 'paper';
  }
}
const userChoice = getUserChoice()
const determineWinner = (userChoice , computerChoice) =>{
  if (userChoice === computerChoice) {
    return 'Tied, Play again!';
  }
 if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'Player You Lose!'
    }else {
      return 'Player Wins!'
    }
  }
 if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'Player You Lose!'
    }else {
      return 'Player Wins!'}
  }
  }
   if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'Player You Lose!' ; 
    } else {
      return 'Player Wins!';
      }  
    }

const playGame = () => {
  const userChoice = getUserChoice('Paper');
  const computerChoice = getComputerChoice();
  console.log('You threw ' + userChoice);
  console.log('The computer threw ' + getComputerChoice)
  console.log(determineWinner(userChoice, computerChoice))
}

This is the error I keep getting on output

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:3
userInput = userInput.toLowerCase();
^

TypeError: Cannot read property ‘toLowerCase’ of undefined
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:3:24)
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:22:20)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

Hi, so from what I can see its just a few semi colons missing here and there and some unused variables. It took me a while but here it is, It corrected your code and removed this line :

const userChoice = getUserChoice()

Which doesn’t seem to do anything. And also on your first method the userInput should not be wrapped into parentheses. So just compare it with your code, so you can see where you might have missed a thing or two.
Try using your identation and space in between your blocks of code, it can help a lot for readability.

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 'scissors'; case 2: return 'paper'; } } const determineWinner = (userChoice , computerChoice) => { if (userChoice === computerChoice) { return 'Tied, Play again!' } if (userChoice === 'rock') { if (computerChoice === 'paper') { return 'Player You Lose!'; }else { return 'Player Wins!' } } if (userChoice === 'paper') { if (computerChoice === 'scissors') { return 'Player You Lose!'; }else { return 'Player Wins!' } } if (userChoice === 'scissors') { if (computerChoice === 'rock') { return 'Player You Lose!' ; } else { return 'Player Wins!' } } }; const playGame = () => { const userChoice = getUserChoice(); const computerChoice = getComputerChoice(); console.log('You threw ' + userChoice); console.log('The computer threw ' + getComputerChoice); console.log(determineWinner(userChoice, computerChoice)); } console.log(determineWinner('paper', 'scissors'));

Let me know if this works for you.

1 Like

Worked great, thank you! I’ll keep readability in mind

1 Like

No worries, happy coding !