'Rock, scissor, paper' Cannot read property 'toLowerCase' of undefined


#1

Hi there! I just finished the whole code but when I call playGame function this is what happens:
TypeError: Cannot read property ‘toLowerCase’ of undefined
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:2:23)
at playGame (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:57:20)
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:64: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)

But when I call getUserChoice function, it works. Can you see the problem?

const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’) {
return userInput;
}
else {
return ‘Oops! Try again!’;
}
};


#2

What argument are you calling the function with?


#3

methods (properties) belong to specific data type, so userInput has to be string, it seems to be undefined at the moment


#4

When I call getUserChoice() I use strings ‘rock’, ‘scissors’, ‘floor’ . The same when I call playGame()


#5

can we see the full code?


#6
const getUserChoice = (userInput) => {
 userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  }
  else {
    return 'Oops! Try again!';
  }
};

const getComputerChoice = () => {
  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 'It`s a tie!';
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'Computer wins!';
    }
    else {
      return 'You win!';
    }
  }
  if (userChoice === 'paper'){
    if (computerChoice === 'scissors') {
      return 'Computer wins!';
    }
    else {
      return 'You win!';
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'Computer wins';
    }
    else {
      return 'You win!';
    }
  }
};

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


 
console.log(playGame('rock'));

#7

You need to pass userChoice

getUserChoice(userChoice)

#8

Thank you! Now it doesn’t show me type error, but ‘undefined’. Trying to solve this new problem.


#9

When you call this function, only pass in the user’s choice, and let the program retrieve the computer choice.

const playGame = userChoice => {

};

One thing you could do to make it more obvious would be assign the userChoice that is sent back from the check function.

userChoice = getUserChoice(userChoice)

#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.