Https://www.codecademy.com/courses/introduction-to-javascript/projects/rock-paper-scissors-javascript

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

https://www.codecademy.com/courses/introduction-to-javascript/projects/rock-paper-scissors-javascript

For some reason my code will not accept my function. It keeps saying it is undefined. Its not taking into account the if statements. This is what I percieve the problem to be. I also have the code here on hastebin for viewing.

console.log(
  "hello and welcome to the game rock, paper, scissors! Please choose rock, paper, or scissors!"
);

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if (
    userInput === "rock" ||
    userInput === "paper" ||
    userInput === "scissors"
  ) {
    return userInput;
  } else
    console.log("Invalid selection. Please choose rock, paper, or scissors");
};

console.log(getUserChoice("rock"));

const getComputerChoice = () => {
  console.log(Math.floor(Math.random() * 3));
  if (getComputerChoice === 0) {
    return "rock";
  } else if (getComputerChoice === 1) {
    return "paper";
  } else {
    if (getComputerChoice === 2) {
      return "scissors";
    }
  }
};
console.log(getComputerChoice());

Hi !

You don’t actually save the value of the computer choice in your function:

console.log(Math.floor(Math.random() * 3));

And then you reference the function name in your if/else statement:

if (getComputerChoice === 0) {
    return "rock";
  } else if (getComputerChoice === 1) {
    return "paper";
  } else {
    if (getComputerChoice === 2) {
      return "scissors";
    }

Try to save the result of the math operation in a variable like you did with the user input in your first function !

1 Like