Project Rock Paper Scissors

Hello All,
Many thanks in advance for your help. Before i check the video i manage to obtain a similar result but not the same. As well the code could be much simplier and not writing that many lines…but i would like to understand why is showing undefined after the lastest sentence.

Code:

const getUserChoice = userInput =>{
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  } else {console.log("There is an error in your input, please write one of the following options: rock, paper or scissors. Many thanks.")}
};

const getComputerChoice = () =>{
  let 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 console.log("The game was tie");}
  else if (userChoice === 'rock' && computerChoice === 'paper'){
    return console.log("Computer has won");}
 else if (userChoice === 'rock' && computerChoice === 'scissors'){
    return console.log("Human has won");}
  else if (userChoice === 'paper' && computerChoice === 'scissors'){
    return console.log("Compute has won");}
  else if (userChoice === 'paper' && computerChoice === 'rock'){
    return console.log("Human has won");}
  else if (userChoice === 'scissors' && computerChoice === 'paper'){
    return console.log("Human has won");}
  else {
    return console.log("Computer has 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();

End code

imagen

Best regards,
Carlos

Hey,

You are getting an undefined because you use console.log on a function that returns a console.log :wink:

Inside playGame

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

To solve this you either call the function

determineWinner(userChoice, computerChoice));

Or have the determineWinner function return strings

  if (userChoice === computerChoice) {
    return "The game was tie";
  } 

and call console.log on the determineWinner function as it is now.

Happy coding!

1 Like