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)