Rock paper scissors

if (userChoice === ‘rock’ && computerChoice === ‘paper’) {
return ‘Computer wins’;
} if else (userChoice === ‘scissors’ && computerChoice === ‘rock’) {
return ‘Computer wins’;
} if else (userChoice === ‘paper’ && computerChoice === ‘scissors’) {
return ‘Computer wins’;
} else {
return ‘Player wins’
}

I know this isnt the way the suggest to do it but just curious as to why this does notwork and throws a syntax error on the 2nd computer choice condition?

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Multiple statements are nested as else if clauses.
You have written them as if else which is not valid syntax.
Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if…else#description

// You wrote:
if (userChoice === ‘rock’ && computerChoice === ‘paper’) {
    return ‘Computer wins’;
} if else (userChoice === ‘scissors’ && computerChoice === ‘rock’) {

// It should be:
if (userChoice === ‘rock’ && computerChoice === ‘paper’) {
    return ‘Computer wins’;
} else if (userChoice === ‘scissors’ && computerChoice === ‘rock’) {
1 Like

Its so simple the syntax error had arrows under the computerChoice part so I did not think this would have been the problem, such a stupid mistake, how can I go about solving issues like this myself in the future?

It’s difficult even for seasoned programmers to notice typo and bracket errors, especially in larger projects. When you move to using a code editor later in the coursework, they will help you, plus the more experience you get of seeing certain errors, the more you’ll know to check for simple errors.

1 Like

Makes sense, thank you its the most frustrating thing when its something so simple but your brain just doesn’t pick it up