Can't figure out why it's printing this to the console


#1

I don’t think my ‘computer choice’ section is working properly, when i try and run the program it just spits out the whole block instead of doing the calculation and return. I’m sure it’s something stupid but i can’t for the life of me find the bug, can anyone tell me where i went wrong?

Here’s the code:

//user input section
const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  } else {
    console.log('Invalid Input');
  }
}
//computer input section
const getComputerChoice = () => {
let randomNumber = Math.floor(Math.random()*3);
switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2: 
      return 'scissors';
     }
}
//winner calculation
const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'Tie';
  } 
  if (userChoice === 'rock'){
    if (computerChoice === 'paper') {
      return 'Computer wins';
    } else { return 'You win';}
  } 
  if (userChoice === 'paper'){
    if (computerChoice === 'scissors'){
      return 'Computer wins';} else {return 'You win';}
    }
  if (userChoice === 'scissors'){
    if (computerChoice === 'rock'){
      return 'Computer wins';} else {return 'You win';}
  }
  }
//play function
const playGame = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice;
  console.log(`You threw ${userChoice}`);
  console.log(`Computer threw ${computerChoice}`);
  console.log(determineWinner(userChoice, computerChoice));
}

playGame();

And here is the console.log:

You threw scissors
Computer threw () => {
let randomNumber = Math.floor(Math.random()*3);
switch (randomNumber) {
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 2:
return ‘scissors’;
}
}
You win


#2

The getComputerChoice function is not invoked, but rather copied to computerChoice. Consequently, when it is logged, the source code of the function is what gets sent to the display.

const computerChoice = getComputerChoice();

#3

Omg Roy thank you so much!
Makes sense now that i see it. Welp, looking forward to not making as many small mistakes in the future!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.