Cannot read property toLowerCase function

Hello Everyone
I am Doing the Rock,Paper, Scissors Game Exercise, I have Got a Error and couldn’t find the solution
below is a snap of the code and and the error.

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

getUserChoice('rock');
//console.log(getUserChoice('rock'))

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';
    break;
  }

};


const determineWinner = (userChoice , computerChoice) => {
  if(userChoice === computerChoice){
    return 'Game Was a Tie';
  }else if(userChoice === 'rock'){
    if(computerChoice === 'paper'){
      return 'The Computer Won';
    }else{
      return 'You won';
    }
  }

  if(userChoice === 'paper'){
    if(computerChoice === 'scissors' || computerChoice === 'rock'){
      return 'the computer won';
    }
    else{
      return ' You Won';
    }
  }

  if(userChoice === 'scissors'){
    if(computeChoise === 'rock'){
      return 'The computer Won!';
    }else{
      return 'You Won!';
    }
  }
}

// console.log(determineWinner('paper', 'scissors')); // prints something like 'The computer won!'
// console.log(determineWinner('paper', 'paper')); // prints something like 'The game is a tie!'
// console.log(determineWinner('paper', 'rock')); // prints something like 'The user won!'




const playGame = () => {
  const userChoice = getUserChoice();
  const computerChoice = getComputerChoice();

    console.log('You threw: ' + userChoice);
    console.log('The computer threw:' + computerChoice);

 console.log(determineWinner(userChoice, computeChoice));

}

playGame();




and here is a snap of the error i got




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

TypeError: Cannot read property 'toLowerCase' of undefined
    at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:2:25)
    at playGame (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:68:22)
    at Object.<anonymous> (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:78: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)















1 Like

Hello @devaqa, welcome to the forums! The problem is here:

The getUserChoice function accepts one argument, but here you provide it with no arguments. In JS, unlike many other programming languages, any unassigned arguments will be automatically given the value undefined, which is sort of its own data type, with which many functions can’t be used with, such as .toLowerCase.
I hope this helps!

2 Likes