Hi all,
This is my first post on the forum and sorry if it doesn’t follow all the guidelines :).
I am a beginner and I need your help to understand this topic.
I am having the following error when I run my program for the project below:
TypeError: Cannot read property ‘toLowerCase’ of undefined
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:3:24)
at playGame
Below is my code:
`[codebyte]
console.log("Hi");
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (
userInput === "rock" ||
userInput === "paper" ||
userInput === "scissors"
) {
return userInput;
} else {
console.log("Not a valid Choice");
}
};
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 "This game is a tie";
}
if (userChoice === "rock") {
if (computerChoice === "paper") {
return "Sorry, the computer won!";
} else {
return "Congratulation, you win the game!";
}
}
if (userChoice === "paper") {
if (computerChoice === "scissors") {
return "You lost the game!";
} else {
return "Congratulation, you won!";
}
}
if (userChoice === "scissors") {
if (computerChoice === "rock") {
return "You have lost, sorry!";
} else {
return "Well played!";
}
}
};
// playgame function
const playGame = () => {
const userChoice = getUserChoice('paper');
const computerChoice = getUserChoice();
console.log('You threw' + userChoice)
console.log('The computer threw' + computerChoice)
};
playGame()
//console.log(determineWinner('scissors', 'scissors'))
``[/codebyte]`
I can’t find any solution and the code seems to be working fine up to the point I have declared the playGame function.
What is the reason for this error?