Hi everyone!
Ive been working through the Rock, Paper, Scissors exercise (https://www.codecademy.com/courses/introduction-to-javascript/projects/rock-paper-scissors-javascript) but I
m getting an undefined error right after I try to log the playGame function (task 13).
Here is my code:
const getUserChoise = userInput =>{
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' ){
return userInput
}else{
console.log('Error, please type: rock,paper or scissors.');
}
}
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, computer won!.";
}else{
return "Congratulations, you won!";
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors'){
return "Sorry, computer won!";
}else{
return'Congratulations, you won!';
}
}
if(userChoice === 'scissors'){
if(computerChoice === 'rock'){
return "Sorry, computer won!";
}else{
return "Congratulations, you won!";
}
}
};
const playGame = () => {
const userChoice = getUserChoice('scissors');
const computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice,computerChoice));
};
playGame();
When I called the playGame function on the last line of my program, I got the message below:
/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:51
const userChoice = getUserChoice('scissors');
^
ReferenceError: getUserChoice is not defined
at playGame (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:51:23)
at Object.<anonymous> (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:59: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)
at startup (bootstrap_node.js:151:9)
Thanks!