Rock, Paper, Scissor Project

Hello, I need help with steps 10 and 11 of this project: https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/learn-javascript-functions/projects/rock-paper-scissors-javascript

I have no idea where the mistake is. Until the end of the switch inside the getComputerChoice function, it everything was fine, but, after that, there is always a mistake indicating userChoise doesn’t exist, or that there is a “syntax error )”.

This is the error message:

*/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:60*
*});*
* ^*
*SyntaxError: Unexpected token )*
*    at createScript (vm.js:53:10)*
*    at Object.runInThisContext (vm.js:95:10)*
*    at Module._compile (module.js:543:28)*
*    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)*

And this is my actual code:

const getUserChoice = userInput => {

  userInput = userInput.toLowerCase(); 

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

    return userInput;

         } else {

             console.log('Error!');

             }

         };

function getComputerChoice() {

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

  switch (randomNumber) {

  case 0:

  return 'rock';

  break;

  case 1:

  return 'paper';

  break;

  case 2: 

  return 'scissors'

  default:  alert ("Choose among 0, 1 and 2");

  break ;  }

  };

function determineWinner(userChoice, computerChoice) {

  if (userChoice === computerChoice) {

    return 'The game is a tie!';

  } 

  if (userChoice === 'rock') {

  if (computerChoice === 'paper') {

    return 'The computer won!';

  } else {

    return 'You won';

   } 

  };

  if (userChoice === 'paper') {

   if (computerChoice === 'scissors') {

     return 'The computer won!';

   } else {

     return 'You won!';

    }   

  };

  if (userChoice === 'scissors') {

    if (computerChoice === 'rock') {

      return 'The computer won!';

  } else {

    return 'You won';

   } 

  };

console.log(determineWinner('scissors', 'rock'));

Any help is very welcome. Maybe its an error about the different forms of writing funcions?

Thank you all in advance.

Hello @pedroa9492701646, welcome to the forums! It seems you are just missing a closing } in your determineWinner function. Also, when you write functions defined with a function keyword, you shouldn’t out a semicolon after the closing }. As a general rule, semicolons should not be placed with }, unless you have a function with arrow syntax:

const a_function = () => {
//code
}; //this is ok
function a_function() {
//code
};//this is NOT ok-don't put a semicolon
if (true) {
//code
}; //this is NOT good-don't put a semicolon

I hope this helps!

1 Like

Thank you for your fast reply.

I added the missing “}” and took of the semicolon on variables, leaving it only to close an arrow syntax function. Now, my code is like this:

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

const getComputerChoice = () => {
 const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
     return 'rock';
      break;
    case 1:
    return 'paper';
      break;
   case 2: 
     return 'scissors';
     break;
  default:
     console.log ('choose 0, 1 or 2');
   }
  };

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'The game is a tie!';
  } 
};

  if (userChoice === 'rock') {
   if (computerChoice === 'paper') {
    return 'The computer won!';
  } else {
    return 'You won';
   } 
  }

  if (userChoice === 'paper') {
   if (computerChoice === 'scissors') {
     return 'The computer won!';
   } else {
     return 'You won!';
    }   
  }

  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
  } else {
    return 'You won';
   } 
  }

console.log(determineWinner('paper', 'scissors')); // prints something like 'The computer won!'

The error message now changed to:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:33
  if (userChoice === 'rock') {
      ^

ReferenceError: userChoice is not defined
    at Object.<anonymous> (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:33:7)
    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)
    at bootstrap_node.js:542:3

I watched the resolution video, but found no different with my code.

Hello @jhonathanc. The reason for this is that the if statements are outside of the function determineWinner. This means they aren’t using the parameters userChoice and computerChoice. As seen here:

You have closed the function after only the first if statement.

1 Like

Nice, thank you very much.

1 Like