Need help finding bug in Rock, paper, scissors

Here is the code:

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

//console.log(getUserChoice('rock')); //used to test 'getUserChoice'

const getComputerChoice = () => { 
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
  case 0:
  return 'rock';
  break;
  case 1: 
  return 'paper';
  break;
  case 2: 
  return 'scissors';
}
};
/*/console.log(getComputerChoice());
console.log(getComputerChoice());  --> testing getComputerChoice()
console.log(getComputerChoice());/*/

 const determineWinner = (userChoice, computerChoice) => {
if(userChoice === computerChoice) {
  return 'Tie! No winner. Try again!';
} else {
if(userChoice === 'rock') {
  if(computerChoice === 'paper') {
    return 'Sorry, the computer has won! Try again!'
  } else {
    return 'Congratulations! you won!'
  }
} 
if(userChoice === 'paper') {
  if(computerChoice === 'scissors') {
    return 'Sorry, the computer has won! Try again!'
  } else {
    return 'Congratulations! you won!'
  }
} 
if(userChoice === 'scissors') {
  if(computerChoice === 'rock') {
    return 'Sorry, the computer has won! Try again!'
  } else {
    return 'Congratulations! you won!'
  }
} 
 }};

/*console.log(determineWinner('rock', 'scissors'));
console.log(determineWinner('rock', 'paper'));
console.log(determineWinner('rock', 'rock'));*/
const playGame = () => {
  let userChoice = getUserChoice();
  let computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
};

playGame();

getting the following error and can’t figure out what is the problem. Please help.

/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:2
userInput = userInput.toLowerCase();
^

TypeError: Cannot read property ‘toLowerCase’ of undefined
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:2:24)
at playGame (/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:60:20)
at Object. (/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:67:1)
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)

We need to see your code. Best be you post it here, properly formatted.
The link above just leads to the exercise.

Thank you! Edited and added code.

Please also format your code:

Thank you again. my first time asking for help on forum! Let me know if this worked ok!

1 Like

getUserChoice expects an argument:

But you call it without one:

So the parameter userInput is undefined:

  userInput = userInput.toLowerCase();

1 Like

Ah, we had to add the argument ‘paper’ because it wasn’t a random number like the computerChoice was. Does that sound right? I added paper to:
let userChoice = getUserChoice(‘paper’);

Thank you!

1 Like