Why am I getting an unexpected token error? (Rock, Paper, Scissors)

For some reason, I’m getting an ‘undefined token’ error in my console pointing to my const relating to playGame. I can’t seem to figure out why. Could someone please explain? Thanks.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput
  } else{
    console.log('ERROR!')
  }
};
const getComputerChoice = () => {
  randomNumber = Math.floor(Math.random()*3)
  switch(randomNumber) {
    case 0:
      return 'rock';
      break;
    case 1:
      return 'paper';
      break;
    case 2:
    return 'scissors';
    break;
  }
}
const determineWinner = (userChoice, getComputerChoice => {
  if (userChoice === getComputerChoice) {
    return 'Tie!';
  }
  if (userChoice === 'rock') {
    if (getComputerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'The user won!';
    }
  }
  if (userChoice === 'paper') {
    if (getComputerChoice === 'rock') {
      return 'The user won!';
    } else {
      return 'The computer won!'
    }
  }
  if (userChoice === 'scissors') {
    if (getComputerChoice === 'paper') {
      return 'The user won!';
    } else {
      return 'The computer won!';
    }
  }
}

const playGame = () => {
  console.log(determineWinner(2, getComputerChoice));
}
playGame();

Take a look at your determineWinner function on line 24; does that look right to you?

(The error message is not always exactly where the issue is; it’s usually not a bad idea to look at what comes before it as well.)

3 Likes

That is kind of an ambiguous error message. It means your code is basically trying to do something like this:

> const a = const b
const a = const b
          ^^^^^

SyntaxError: Unexpected token const

Or in your case more like this:

> const c = (//bunch of code
... //more code
... //more code
... //still more code
... //and just a tad more code
... const d = 5;
const d
^^^^^

SyntaxError: Unexpected token const
3 Likes

Oh right ty. Even when it’s corrected though, it just outputs ‘undefined’.

Try printing your input to see if it is what you expected:

userInput just comes out as an undefined variable even though I gave it a value on line 51.

Let’s walk this back a little bit. What type of input is your determineWinner function expecting?
If we look inside the function, you are making several comparisons. You are comparing userChoice to 'rock', 'paper' and 'scissors', correct? That in mind, does this function call supply expected values as arguments?

Also, keep in mind if we want the return value of a function rather than the function itself, we need to invoke it.

Hi,
Im having the same problem! with the variable randomNumber
can anyone help?

const getUserChoice = userInput => {
userInput = userInput.toLowerCase ();
if (userInput === ‘rock’ || userInput === ‘paper’ ||userInput === ‘scissors’){
return userInput
}else {
console.log(‘error message’);
}
}
const getComputerChoice = ( ) => {
const randomNumber = (Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 2:
return’scissors’;
}
};
console.log(getComputerChoice());

Yeah userChoice is meant to be comparing to rock, paper and scissors. I figured that by putting 2 in determineWinner(2, getComputerChoice)); the code would know that usetInput should be there hence replacing userInput with 2 in the determineWinner function.

Your error message is: SyntaxError: Unexpected token ;
It refers to this line:

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

We would typically want our () in pairs. What would the computer be expecting rather than ;?

1 Like

By supplying 2 as an argument along with getComputerChoice you end up with the following comparison:

if( 2 === [Function])
if(2 === 'rock')
//etc.

Those will never evaluate to true. You need to supply arguments like 'rock', 'paper' or 'scissors'.
You have functions that return those very values.

ahh thankyou! Couldn’t spot it

1 Like

Oh! I see what you mean. Wow that was silly of me. Thank you. Although, now if I input something like ‘paper’ for userInput, it just constantly outputs ‘The computer won!’. Shouldn’t it be referring back to the random number code and be randomly selecting a word each time?

You should be using your getUserInput function to validate your user input. You also want the return value of the getComputerChoice function rather than [Function] for the computer choice. Your call to determineWinner should have those two values as its arguments.

Hint:

First argument: getUserChoice('rock') //or 'paper' or 'scissors'
Second argument: getComputerChoice()

1 Like

I would also suggest using a variable that is not the same name as the function.

determineWinner(userChoice, computerChoice)

The latter argument will refer to the outcome of getComputerChoice() just as the first argument refers to the outcome of getUserChoice().

3 Likes

Sorry this is taking so long. I already have getComputerChoice as my second argument. And if I put getUserChoice(‘Rock’) as my argument inside determineWinner, should it actually work? Then you’re already giving the value inside the function instead of later on giving the value such as I did with paper at the bottom. Sorry, this is all just really confusing me :joy:Screen Shot 2020-10-06 at 20.29.40

The name of the function is not the return value of the function. You need to call the function.
Add these lines at the very end of your code, and note the different outputs.

console.log(getComputerChoice);
console.log(getComputerChoice());

Note also that @midlindner pointed out the validation process involved with getUserChoice(). We should not be using the string object directly in the call to determineWinner()

Consider,

determineWinner(getUserChoice('paper'), getComputerChoice())
1 Like

Oh I see. I didn’t realise that was even a thing. I must have missed that / forgotten that. I added the () to getComputerChoice in determineWinner yet the outputs are completely wrong. It will tie at random times for example.

Show us your code, please.