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

Hey,

I’m stuck on the rock, paper, scissors project. I get the following error, but I can’t figure out how to fix it: /home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:2
userInput = userInput.toLowerCase();
^

TypeError: Cannot read property ‘toLowerCase’ of undefined
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:2:24)
at playGame (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:47:16)
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:54:1)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)

I would appreciate any help :slight_smile:

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

Could you post your code so we can look into it? Use the </> button to post your code.

For the time being, the error message says it all, userInput is undefined. Which means you are either not passing an argument into the function or have a typo somewhere.

Happy coding!

1 Like

Hey, here’s the code I have:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } else {
    console.log('Error');
  }
};
const getComputerChoice = () => {
  let randomNumber = Math.floor(Math.random() * 3);
  if (randomNumber === 0) {
    return 'rock'; 
  } else if (randomNumber === 1) {
    return 'paper';
  } else {
    return 'scissors';
  }
};
const determineWinner = (userChoice, computerChoice) => {
  if(userChoice === computerChoice) {
    return 'The game was tied.';
  }
  if(userChoice === 'rock') {
    if(computerChoice === 'paper') {
      return 'The computer won';
    } else {
      return 'The user won';
    }
  }
  if(userChoice === 'paper') {
    if(computerChoice === 'rock') {
      return 'The user has won';
    } else {
      return 'The computer won';
    }
  }
  if(userChoice === 'scissors') {
    if(computerChoice === 'rock') {
      return 'The computer won';
    } else {
      return 'The user won';
    }
  }
}

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

playGame();

Like I predicted. You are not passing any arguments into the getUserChoice function. See:

const getUserChoice = userInput => { // expects a userInput
userChoice = getUserChoice(); // but you call it without, causing an undefined.

You will have to provide userInput. Either do:

function playGame() {
  userChoice = getUserChoice('sciccors'); // and modify your choice here
  computerChoice = getComputerChoice();
  console.log(userChoice);
  console.log(computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}

Or make playGame take the userInput as argument and modify the function slightly.

function playGame(userInput) { // make it have one input variable
  userChoice = getUserChoice(userInput); // process it here as the input for getUserChoice
  computerChoice = getComputerChoice();
  console.log(userChoice);
  console.log(computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}

playGame('sciccors'); // and provide your choice upon calling playGame

The last one would be my choice since it makes more sense. But the exercise takes in the first option as solution.

Happy coding!

1 Like

It works now. Thanks so much :slight_smile: