Rock, paper, scissors project | Javascript | 'undefined'


#1

this lesson right here

I’m trying to understand why the console returns ‘undefined’ at the end, after the code seems to work properly:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } else {
    console.log('Invalid User Input');
  }
};

function getComputerChoice() {
  let computerChoice = Math.floor(Math.random() * 3);
  switch (computerChoice) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
      default:
      return 'error';
  }
};

function determinWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'Tie!';
  } else if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'Computer wins!'
    } else {
      return 'You win!'
    }
  } else if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'Computer wins!'
    } else {
      return 'You win!'
    }
  } else if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'Computer wins!'
    } else {
      return 'You win!'
    }
  } else {
    return 'Error!';
  } 
};

function playGame() {
  let userChoice = getUserChoice('rock');
  let computerChoice = getComputerChoice();
  console.log(`You picked ${userChoice}!`);
  console.log(`Computer picked ${computerChoice}`);
  console.log(determinWinner(userChoice, computerChoice));
}

console.log(playGame());

/*
returns :
“You picked rock!
Computer picked paper
Computer wins!
undefined”
*/


#2

You tried to print your playGame function why?
You would need to call the playGame function not print it


#3

you’re right :slight_smile:
Thanks!