Rock, paper or scissors

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (
    userInput === "paper" ||
    userInput === "rock" ||
    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";
  }
};

console.log(getComputerChoice());

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return "game was a tie";
  }
  if (userChoice === "rock") {
    if (computerChoice === "paper") {
      return "sorry, computer won";
    } else {
      return "congratulations you won";
    }
  }
  if (userChoice === "paper") {
    if (computerChoice === "scissors") {
      return "sorry, computer won";
    } else {
      return "congratulations you won";
    }
  }
  if (userChoice === "scissors") {
    if (computerChoice === "rock") {
      return "sorry, computer won";
    } else {
      return "congratulations you won";
    }
  }
};

const playGames = () => {
  const userChoice = getUserChoice();
  const computerChoice = getComputerChoice();
  console.log(`You Threw ${getUserChoice}`);
  console.log(`Computer threw ${getComputerChoice}`);
  console.log(determineWinner(userChoice, computerChoice));
};

playGames();

I’ve getting this error: /home/ccuser/workspace/javascript_101_Unit_3_v2/rockPaperScissors.js:2
userInput = userInput.toLowerCase();
^

TypeError: Cannot read property ‘toLowerCase’ of undefined

That’s because you call ‘getUserChoice’. without an argument although it expects one:

So ‘userInput’ is undefined.

2 Likes