Why is my code printing to the console?

Here’s my code:

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

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 'Tie!'
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'You Lose'
    } else {
      return 'You Win!'
    }
  };

  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
        return 'You Lose'
    } else {
      return 'You Win!'
    }
  };

  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'You Lose!'
    } else {
      return 'You Win!'
    }
  }
};

const playGame = () => {
  const userChoice = getUserChoice('paper');
  const computerChoice = getComputerChoice();
  console.log(`You threw ${getUserChoice}.`);
  console.log(`The computer threw ${getComputerChoice}.`);
  console.log(determineWinner(userChoice, computerChoice));  
};

playGame()

Here’s what gets printed to the console:

You threw userInput => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’) {
return userInput;
} else {
console.log(‘Error!’);
}
}.
The computer threw () => {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 2:
return ‘scissors’;
}
}.
You Lose

getComputerChoice and getUserChoice are functions. You haven’t invoked them in the consoles, so they return the functions, not the values.

1 Like

Thanks,
I see where I went wrong.

const playGame = () => {
  const userChoice = getUserChoice('rock');
  const computerChoice = getComputerChoice();
  console.log('You threw' + getUserChoice);
  console.log('The computer threw' + getComputerChoice);
  console.log(determineWinner(userChoice, computerChoice));  
};

These 2 lines:

  console.log('You threw' + getUserChoice);
  console.log('The computer threw' + getComputerChoice);

Should be:

  console.log('You threw' + userChoice);
  console.log('The computer threw' + computerChoice);

I was calling the wrong elements.

Either that or you could’ve called the functions directly in the console:

console.log(`The computer threw ${getComputerChoice()}.`);

But the way you did it now works better because if you invoke the functions in the console the comparison in the last console operates with different values:

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