Paper, rock, scissors confusion


#1

hi there, I am really stuck at paper, rock, scissors. I think it's most likely to do with ; and {. How do you keep track of these when you do nested if / else if?

Here is my code:

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    } else if (choice1 === "rock"); {
        if (choice2 === "scissors") {
            return "rock wins";
        }
    } else {
        return "paper wins";
    } else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    } else if {
        (choice1 === "paper") {
            if (choice2 === "rock"); {
                return "Paper wins";
            } else {
                return "Scissors wins";
            }
        }
    }
}
}

Many thanks,
izabela


#2

Good Morning Izabela!

Nicely formatted code; You're right, there is a lot that could go wrong there and it is difficult to keep track. This is why I want to recommend this topic to anyone faced with these situations, whether a mistake was made in the syntax or not, it's a great resource to test that your code is written correctly.

Though it would be great if you could read the linked topic I can point out some of the mistakes I see here.

Immediately I see you misplaced your third else if statement which is just an else statement. Further down I noticed something peculiar as well.

What happened here? Expression comes before brackets. Again take a look here and this will more than likely take care of any written mistakes, remember to go through the list of errors from top to bottom, because one error early on can cause another message further down.

Well this would depend on which text editor you use to write code, but Sublime Text will highlight the brackets associated with eachother. If you select a closing or opening bracket, it'll show you where the associated closing bracket is. This still requires you to make sure there is a matching pair of brackets for each statement either inside or outside, because they can steal them from one another.

Working with normal methods, the best way to keep track is to count them for each indentation systematically; this is why we indent our code.

  1. For each statement, look at the end of the line to see if there is an opening bracket

    if (choice1 === choice2) { <- Check

  2. Then move on to the next line on the same indentation level and check for a closing bracket. If you find another statement or opening bracket first, you know you're missing one.

    Check! -> } else if (choice 1 === "rock"); {

Do this for each indentation, and move on further into the statements, if you're jumping from the first line indentation and skipping to the next indentation before looking for the closing bracket, it can be difficult to keep count.


#3

Ok I formatted your code by using backtiks and jsbeautifier.org (no changes in content). This should increase the readability as you can see when a block of code {} is started and when it ends.

As far as I can see there are 3 different versions used on how to write the else if cases :smile:
2 of them cause errors and 1 is actually correct. Let's start with the one that is correct as it probably makes it easier to see the errors in the other ones (you could also have a look at the hint of 6/9):

    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    }

This is actually how it should look like. You check all possible options for choice1 with the if and the else if (tie,rock,paper,scissors) and what is left for choice2 is checked inside of the else if. As the tie case is handled in a separate if you only have to deal with 2 options and therefore have an if and an else here.

Now the not so good ones:

   else if (choice1 === "rock"); {
        if (choice2 === "scissors") {
            return "rock wins";
        }
    } else {
        return "paper wins";
    }

The problem with this one is as you can see that the else is outside of the else if, that way it is not an alternative for the if which handles choice2 but an alternative to the else if which handles choice1. As this is the first else if it is even worse as it handles far more cases than it should namely every option where choice1 is neither rock nor tie. Now as it handles everything that was not dealt with in previous cases it also finishes the conditional statement. Therefore you'll get an error for the next upcoming else if because you can't have an else without an if.

Last but not least this case:

    else if {
        (choice1 === "paper") {
            if (choice2 === "rock"); {
                return "Paper wins";
            } else {
                return "Scissors wins";
            } 
        }
    }
}
}

Ok here are a whole bunch of problems. First the else if is missing a condition

 else if {

And no this condition

(choice1 === "paper") {

comes after the { so it doesn't count and even worse it has it's own { which doesn't make sense either. You have examples on how this should look like so let's move on inside of the else if:

if (choice2 === "rock"); {

here you have a semicolon after the condition, actually that is the only semicolon mistake I could find here but it is a really crucial one. Wrote a longer explanation what it does and why it is evil when used unintentionally over here:

And the last thing is that you used an additional } to close the function as you can see the last two are on the same line so one is redundant.
Hope this helps.


#4

Thanks all, you messages are very useful!! Will be now going through all of the code.