Computer never picks rock! Error with the pre-set computerChoice variable


Hi there :smile: (my first contribution here)

I was playing around with the "Rock, Paper, Scissors" game after having finished it and I found out that the computer NEVER picks "rock", even after running the game over 20 times (so it's not just a matter of probabilities).

Since computer programming is a lot about debugging I tried to figure out what was wrong.
Here is what I came up with:

There is an error in the pre-written computerChoice variable.

Suppose the function Math.random() returns a value between 0 and 0.33,
according to the first IF statement, the value "rock" is attributed to the variable computerChoice.
But then, according to the ELSE IF statement, computerChoice is gonna be overwritten with the value "paper"!

As a result, the computer has 2/3 chances to pick paper and 1/3 chance to pick scissors.

So now I've replaced the original statement by this one:
else if (computerChoice <= 0.67 && computerChoice >= 0.34) { }
And it seems to work fine!


No actually else if works like this:

// this is just run in case condition was not true

So in the end else if is just a nested if that is why only one case can be chosen. So if you picked "rock" that's it. If you use 3 if's instead this could happen because each of them is interpreted independent of the rest but as said for else if you have a clear dependency which leads to the first case being chosen.

But if you really feel that there is something wrong maybe post your code because otherwise it's just possible to guess.