Computer Choice 2. (syntax)


#1

Hello everyone i was wondering if you could help me out with these lines of code. It seems I am missing a before statement? When run through developer mode i am told i am missing a ";" could someone please help explain this to me. Any hints on where my error lies would be greatly appreciated as I want to learn how to correct my mistake. Below is my section of code.

var userChoice= prompt("Do you choose rock,paper, or scissors?");
var computerChoice= Math.random();
if (computerChoice<=.33) {
computerChoice="rock";
}
else if (computerChoice<=.66) {
computerChoice="paper";
}
else (computerChoice<=1) {
computerChoice="scissors";
}


#2

In short:

else (computerChoice<=1) {

else has no condition after it as it is the default case that deals with everything that doesn't fall in one of the categories mentioned earlier (if and else ifs).

The longer version is that its not forbidden to omit the {} after conditions, in this case they just work on the next statement.

So for example:

else (computerChoice<=1)
{
    computerChoice="scissors";
}

would be find BUT this would not work as expected. What it would do is to treat (computerChoice<=1) as the statement that is connected to the else and the stuff in {} would no longer belong to the statement and would therefore be executed every time. This makes the whole statement kind of useless which is on of the reasons this should be used with caution.

Now the reason why it asks for a semicolon is because you written this:

else (computerChoice<=1) {

The {} is a way to wrap many statements in one, so the { is the start of a new statement. If you count now you see that you have two statements ( (computerChoice<=1) and { ) in one line so the interpreter asks for a separator. Which would solve the error but would cause real trouble in your code. Computers are stupid :wink:

So I hope this was not too confusing. In case it is just stick to the short version and get rid of the condition :slightly_smiling:


#3

thank you very much!