Rock paper scissors wont work

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

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

when i run my rock paper scissors program it does not work and shows this message:
/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 Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:55: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)
at startup (bootstrap_node.js:151:9)

what am i doing wrong? here is my code:

const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if(userInput === ‘rock’ || userInput === ‘paper’ || 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’;
}
}
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice)
return ‘Tie!’;
}
if (userChoice === ‘rock’) {
if (computerChoice === ‘paper’) {
return ‘You lose’;
} else if (userChoice === ‘rock’ && computerChoice === ‘scissors’) {
return ‘you won!’;
}
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘paper’) {
return ‘you win’;
} else if (userChoice === ‘scissors’ && computerChoice === ‘rock’) {
return ‘you lose’
}
}
if (userChoice === ‘paper’) {
if (computerChoice === ‘rock’) {
return ‘you win’;
} else if (userChoice === ‘paper’ && computerChoice === ‘scissors’) {
return ‘you lose’;
}
}
var userChoice = ‘’;
const playGame = () => {
const userChoice = getUserChoice();
const computerChoice = getComputerChoice();
console.log('You threw: ’ + userChoice);
console.log('The computer threw: ’ + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
};

getUserChoice();

That call should include an argument, else the function sees ‘undefined’.

The first clue to this is ‘undefined’, and the second is in this line of the error message…

at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:2:24)

That says to look on line 2, around index 24.

Next we look for the call to the function that must be the root of the error, and what do we find?

1 Like

how do i implement the User input?

1 Like

Call the function with an argument, one of ‘rock’, ‘paper’, or ‘scissors’.

getUserChoice('rock');
1 Like

i tried that and it did not work:
**const getUserChoice = (userInput) => {
getUserChoice(rock);
userInput = userInput.toLowerCase();
if(userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’){
return userInput;
} else {
console.log(“error”)
}
};

That line should not be inside the function, but at the bottom where you have it already. Just be sure there is an argument.

1 Like

i put it at the bottom and it still did not work

const getUserChoice = (userInput) => {
getUserChoice(‘rock’);
userInput = userInput.toLowerCase();
if(userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’){
return userInput;
} else {
console.log(“error”)
}
};
getUserChoice(‘rock’);

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:1
(function (exports, require, module, __filename, __dirname) { const getUserChoice = (userInput) => {
^

RangeError: Maximum call stack size exceeded
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:1:85)
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:2:3)
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:2:3)
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:2:3)
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:2:3)
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:2:3)
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:2:3)
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:2:3)
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:2:3)
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:2:3)

You need to delete the second line of the function.

A function that contains a call to itself results in an infinite loop. This is known as recursion and while it can be implemented in a program, it needs careful control so that it at some point actually ends. For another topic down the road.

1 Like

i deleted the second line and it is still not working

Hello, @qu3sting1. Could you re-post your current code using the following instructions?

Code that is posted without the indentations, whitespace and special characters preserved, is very difficult to read, and cannot easily be tested by those trying to help you.

The Codecademy forum provides a quick and easy way to post properly-formatted code. All you need to do is look for the </> icon in the menu bar which appears at the top of the text box while you type.

capture

Press Enter to go to a blank line, click that icon, and you will see this:

capture_r

Just copy and paste your code directly from the editor into that highlighted portion, and all of the nicely-readable formatting will be preserved! Moreover, it will be in a format that can be directly copied and pasted into an IDE for testing.

The same icon can be used for highlighting small code snippets in-line for clarity.

1 Like

Is #2 the line you removed?

Please re-post your new code.

1 Like

yes, #2 is the line that i removed:

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  } else {
    console.log("error")
  }
};

hello? will anyone reply to me

1 Like

The getUserChoice function appears fine. We need to see all of the code.

1 Like

here is all of the code:

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  } else {
    console.log("error")
    }
};
getUserChoice('rock');
const getComputerChoice = () => { 
  const randomNumber =
Math.floor(Math.random() * 3);
  switch(randomNumber){ 
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
}
  }    
const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice)
    return 'Tie!';
}
if (userChoice === 'rock') {
 if (computerChoice === 'paper') {
   return 'You lose';
} else if (userChoice === 'rock' && computerChoice === 'scissors') {
   return 'you won!';
 }
}
if (userChoice === 'scissors') {
  if (computerChoice === 'paper') {
    return 'you win';
 } else if (userChoice === 'scissors' && computerChoice === 'rock') {
   return 'you lose'
 }
}  
if (userChoice === 'paper') {
  if (computerChoice === 'rock') {
    return 'you win';
  } else if (userChoice === 'paper' && computerChoice === 'scissors') {
    return 'you lose';
  }
}
const playGame = () => {
 const userChoice = getUserChoice('rock');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
};

playGame();
2 Likes

I suggest fixing your indentation, match it to your braces. You’ll probably find some issues while doing that which you were hiding from yourself with the arbitrary indentation.

Where things begin and end is not negotiable, and indentation is how you keep track of it. You’re not supposed to be counting braces, that’s far too difficult and far too much effort.

1 Like

Echo what @ionatan said. I copied and pasted your code, and got it to work. Cleaning up the indentation will help immensely. You have a couple of misplaced curly braces. Other than that your code works as expected. When you don’t follow consistent indentation, it is very difficult to locate missing or extra { }'s.

1 Like