Rock paper scissors project getting "undefined"

hi,

I’ve tested each function required for the playGame() but I can’t figure out why the output is this:

paper
User chose undefined
Computer chose paper
undefined

const getUserChoice = userInput => {

  userInput = userInput.toLowerCase();

  if (userInput === `rock` || userInput === `paper` || userInput === `scissors`) {

    console.log(userInput);

  } else {

    console.log ('please enter either "rock", "paper", or "scissors"');

  }

};

// getUserChoice(`huh`)

const getComputerChoice = () => {

  constRandom = Math.floor(Math.random() * 3);

  if (constRandom === 0 ) {

    return "rock";

  } else if (constRandom === 1) {

    return "paper";

  } else if (constRandom === 2) {

    return "scissors";

  }

};

// console.log (getComputerChoice());

const getComputerChoice2 = () => {

  let constRandom = Math.floor(Math.random() * 3);

  switch (constRandom){

    case 0:

      return "rock";

      break;

    case 1:

      return "paper";

      break;

    case 2:

      return "scissors";

      break;  

  }

};

// console.log(getComputerChoice2());

const determineWinner = (userChoice, computerChoice) => {

  //userChoice = `paper`;

  //computerChoice = getComputerChoice();

  //console.log (userChoice);

  //console.log (computerChoice);

  if (userChoice === computerChoice) {

    return "Game was a tie";

  }

  if (userChoice === `rock`) {

    if (computerChoice === `paper`) {

      return `Computer won.`;

    } else {

      return `User won.`;

    }

  };

  if (userChoice === `paper`) {

    if (computerChoice === `scissors`) {

      return `Computer won.`;

    } else {

      return `User won.`;

    }

  };

  if (userChoice === `scissors`) {

    if (computerChoice === `rock`) {

      return `Computer won.`;

    } else {

      return `User won.`;

    }

  };

};

// console.log(determineWinner(`paper`, `rock`));

const playGame = () => {

  const userChoice = getUserChoice(`paper`);

  const computerChoice = getComputerChoice();

  console.log (`User chose  ${userChoice}`);

  console.log (`Computer chose ${computerChoice}`);

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

};

playGame();

here, you call getUserChoice function:

  const userChoice = getUserChoice(`paper`);

and expect a return value, yet when I look at the function, I don’t see any return value?

thank you so much 4H of troubleshooting, and still couldn’t figure it out…but that worked.

Can you explain why I need a “return” keyword for functions?

Am I right to think that if a function such as getUserChoice() will be called later on, must output a value and thus a return must be used, and if a function is to be called and we only want the value output to the console, then there’s no need for the keyword return?

I guess the safest is always to use the keyword return instead of console.log. Please help me understand clearly.

Thnx for the quick help!

yes, you are right. Functions consist of two parts, a function deceleration:

const getUserChoice = userInput => {
   // body of the function
}

which is a set of instruction/code to be executed later.

then there is the function call:

getUserChoice(`paper`);

which will actually execute the function

the .log() method and return keyword are two different things. the .log() method will log to the console, while return hands back data (literally return) the data to the caller/function call

so with return we can pass back data, which is very useful if you consider variable scope

1 Like