Rock Paper Scissors JavaScript function

Hello I’m currently towards the end of the Rock Paper Scissors, project in Learn Java script Syntax (Functions) and am having trouble with the syntax of my function that should determine a winner based on the inputs.

if anyone could help me with this I’d really appreciate it!

function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice){
      return 'this game was a tie';
  }

  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer Won!!'
    } else {
      return 'The user won!'
    }
  }
  if (userChoice === 'paper'){
    if (computerChoice === 'scissors') {
      return 'The computer won!!'
    } else {
      return 'The user won!'
    }
  }
  if (userChoice === 'scissors'){
    if (computerChoice ==='rock'){
      return 'The computer won!!'
    } else {
      return 'The user won!'
    }
  }

What error do you get? Please post your full code

for example, you might be missing a } to close your function, but this simply also could be a copy-pasting mistake

1 Like

Hey Callum.

check if you are missing a } somewhere. While your solution totally works, once you fixed that, i would suggest checking out

Makes it much easier to read/understand.

1 Like

This is the error message, which shows that I have done something wrong with a }, i’m just struggling to find where :stuck_out_tongue: It would also be useful to be able to better read this information as well!

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:78
});
^
SyntaxError: Unexpected token )

My full code is;

const getUserChoice = userInput =>{
 userInput = userInput.toLowerCase();
 //below uses logical operators to check for an appropriate input.
 if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
   return userInput
 } else {
   console.log('Error, please input a valid material.');
 }
}

function getComputerChoice() {
 let randomNumber = Math.floor(Math.random() * 3) ;
  
  switch(randomNumber){
    case 0:
    return 'rock';
    case 1:
    return 'scissors';
    case 2:
    return 'paper';
  }
};



const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice){
      return 'this game was a tie';
  }

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

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

playGame();

(I did continue with the project, because I see that it’s just a syntax issue) :slight_smile:

So I got it right:

determineWinner that is, but that was the only function you initial posted

There is a small course on Debugging:
https://www.codecademy.com/learn/javascript-errors-debugging

For short:
/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:78
Tells you the file and the line “:78” were the interpreter got confused.

And yes, determine function is missing a }.

1 Like