Rock paper scissors

Hey can some one walk me through my own code, console keeps giving me error code but, i cant seem to find the issue.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'paper' || userInput === 'scissors' || userInput === 'rock') {
    return userInput
  } else {
    return 'Error you have picked an invailded choice please try again'
  }
}

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 'The game was a tie';
} 
if (userChoice === 'rock') {
 if (computerChoice === 'paper') {
 return "Sorry, computer won!";
  } else {
    return "Congratuations, you won!";
  } 

if (userChoice === 'scissors') {
  if (computerChoice === 'rock') {
  return "Sorry, computer won!";
} else {
  return "Congratuation, you won!";
}

if (userChoice === 'paper') { 
  if (computerChoice === 'scissors') {
  return "Sorry, computer won!";
} else { 
  return "Congratuation, you won!";
}
}

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

What’s the error code? Debugging better helps us write better code in general. :slight_smile:

when i go to log a winner

console.log(determineWinner(‘paper’,‘rock’));

i keep getting an error code rather than “Sorry, computer won!”; or “Congratuation, you won!”;

Right, I mean can you copy/paste your error code in here? The error code is a hint at what the mistake(s) could be.

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:48
});
^
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)

The two big clues that jump out are:

  • SyntaxError: Unexpected token ) – which means the ) symbol was unexpected by the interpreter.
  • rockPaperScissors.js:48, this is pointing to the line of the code where it happens!

This means the error relates to that parenthesis. It’s either missing its starting partner, or it’s one too many.

line 48 would be

console.log(determineWinner(‘paper’,‘rock’));

i dont see an issue here

Check the vicinity. @data7296308531 In order of importance, make sure all your function brackets close, good indentation practice will make it easier to find in the future.

As a tip, when you start writing a function it’s useful to outline the end so you don’t lose track:

const determineWinner = (x,y) => {

};

Then conditionals inside would get the same treatment, but indented beforehand

const determineWinner = (x,y) => {
  if(condition) {
  }
};

Finally it might look something like:

const determineWinner = (x,y) => {
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return "Sorry, computer won!";
    } else {
      return "Congratuations, you won!";
    } 
  }
};

whether it’s tab or two-space indentation is up to you, just use something consistently. These types of bugs will never be too troublesome to deal with after that. You get the bonus that your code will be more readable when you come back to it.

N.B: The reason the interpreter pointed to the last line as the error was that since the function bracket is still open, it’s still awaiting for it to be closed…

1 Like

thank you for your help

No problem! Welcome to the forums