0

When running the function the console logs the expected result followed by "Undefined" directly below it and I can't figure out why. I am just starting to learn javascript so I am sure this is going to be an easy solve but I'm just not seeing it at the moment.

  userInput = userInput.toLowerCase();
  if(userInput !== 'rock' && userInput !== 'paper' && userInput !== 'scissors') console.log('Inocrrect choice, please try again');
  return userInput;
}

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

const determineWinner = (userChoice, computerChoice) => {
  if(userChoice === computerChoice) {
    console.log(`You chose ${userChoice} and the computer chose ${computerChoice}, you tied!`);
  } else if(userChoice === 'rock'){
      if(computerChoice === 'scissors'){
        console.log(`You chose ${userChoice} and the computer chose ${computerChoice}, you won!`);
      } else {
        console.log(`You chose ${userChoice} and the computer chose ${computerChoice}, you lost!`);
      }
    }
    else if(userChoice === 'paper'){
      if(computerChoice === 'rock'){
        console.log(`You chose ${userChoice} and the computer chose ${computerChoice}, you won!`);
      } else {
        console.log(`You chose ${userChoice} and the computer chose ${computerChoice}, you lost!`);
      }
    }
    else if(userChoice === 'scissors'){
      if(computerChoice === 'paper'){
        console.log(`You chose ${userChoice} and the computer chose ${computerChoice}, you won!`);
      } else {
        console.log(`You chose ${userChoice} and the computer chose ${computerChoice}, you lost!`);
      }
    }
}

const playGame = userInput => {
  const userChoice = getUserChoice(userInput);
  const computerChoice = getComputerChoice();
  console.log(determineWinner(userChoice,computerChoice));
}

playGame('rock');
  • It may be because you're console.logging another console.log. What would happen if instead of console.log(determineWinner(arg1,arg2)) you just call determinewinner(arg1,arg2)? – BGPHiJACK Apr 26 '22 at 12:28
  • Your `determineWinner` function logs within, but you also are logging the return value of the function `determineWinner`, and that returns nothing, hence `undefined`. Just call the function, without logging it. – Steven Spungin Apr 26 '22 at 12:30

0 Answers0