I have been working on learning JavaScript as a hobby and maybe move to professional at some point in the future. I have been going through the basic Codecademy learning path and just got done with functions. I am not signed up for pro but I saw that one of the projects for pro was to code Rock, Paper, Scissors. I wanted to challenge myself and see if I could do it without much help. So far this is what I’ve made:

Better? I suppose that’s a matter of personal preference.
More concise? For sure. That doesn’t necessarily mean better.

You might consider ways you could DRY your code. For instance, you could try utilizing logical operators to eliminate using the same return statements three times each in your compareAnswer function.

x % 3 gives the remainder when dividing by 3 (also called x mod 3)

The mod3 function is just to get x % 3 (but having a result of only 0 or 1 or 2),
but in JavaScript, if x is negative, then x % 3 would be negative (so x % 3 could be -1, -2, 0, 1, 2)
thus add 3 to a negative result to get the appropriate positive result of doing mod 3.

A cycle of 3:

Notice that those are in order:
paper beats rock, scissors beats paper, rock beats scissors.
In all those cases, the winning one is always 1 more (or 2 less) than the losing one;
so the (winner − loser) = 1 or −2 (which would have a result of 1 when doing mod 3).
Also, the losing one is always 1 less (or 2 more) than the winning one;
so the (loser − winner) = −1 or 2 (which would have a result of 2 when doing mod 3).

For example,
if the playerChoice is rock and comChoice is scissors,
then playerChoice - comChoice would be 0 - 2 which is -2 and mod3(-2) is 1 indicating that the player is the winner.
if the playerChoice is rock and comChoice is paper,
then playerChoice - comChoice would be 0 - 1 which is -1 and mod3(-1) is 2 indicating that the player is the winner.

Just a question, what is the motivation in including the statement

const result = x % 3;

I am assuming that playerChoice and comChoice would be random numbers 0, 1, 2
Therefore, playerChoice - comChoice would be one of -2, -1, 0, 1, 2

So, the mod statement doesn’t seem absolutely necessary. In case of negative difference, incrementing the difference by 3 would work.

However, the inclusion of the mod statement does make for better readability. It helps in understanding why an increment of 3 works in cycling from a negative difference to a non-negative difference. Without the mod statement, it may not be clear why 3 has been chosen in the statement result = result + 3;

Was that the motivation or were there other reasons for including the mod statement?

Including this statement in the mod3 function frees us from making assumption that the argument will be an integer in the range -2 to +2. With your statement, mod3 can handle any integer argument and normalize the result to be one of 0, 1, 2.