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();
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
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.
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.
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()
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
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.
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()
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.