4. Computer's Second Choice


#1

This is what I have:

if (computerChoice <= 0.33) {
computerChoice = "Rock";
};
else if (computerChoice <= 0.66) {
computerChoice = "Paper";
};
else (computerChoice >= 0.67) {
computerChoice = "Scissors";
};

This is the error i get:
Oops, try again. There was a problem with your syntax.

This is what it says:
SyntaxError: Unexpected token else


#2

You don't put semicolons within a sequential if statement, you're breaking them up. Semicolons are reserved for separating lines of code segments. The if statement is a chain of conditionals, though you may read them on different lines, they function as one block of code. If you use a semicolon for example, you're telling javascript to forget each conditioanls relationship with one another.

Also remember, else statements don't take conditions, as they are reserved for all other possibitlities. It's automatically assumed that the condition is being considered.


#3

I changed it to this:

if (computerChoice <= 0.33) {
computerChoice = "Rock"
};
else if (computerChoice <= 0.66) {
computerChoice = "Paper"
};
else {
computerChoice = "Scissors"
};

and it still didn't work


#4

You still have those semicolons. They only belong at the end of a code block.

If () {}
else if () {}
else {};  // End of if code block

#5

oh thanks! it worked!


#6

It solves mine too, thanks :laughing:


#7

@acacio360 Always glad when an answer helps out others!

Shall I Clarify?

Let me take the opportunity to rephrase my answer a bit more for anyone else who comes here. If you don't yet understand, do not write your if statements on completely new lines as was done in this threads first post. Instead write your if statements like this:

if () {
} else if () {
} else {};

This will help reinforce your understanding of where your semicolons should go, and avoid confusion. You wouldn't place a semicolon after each bracket if you had your statement directly following it. You can even do this

if () {} else if () {} else {};

See how this shows what your if statement actually is as a regular statement that exists on one line just like declaring your variables.

var foo = undefined;

You place semicolons to signify the end of a statement. Placing them between your if statement conditions interrupts the relationship of that statement. This is what allows you to write multiple statements on a single line, and by doing this is how we are able to minify our JavaScript by removing all whitespace & line breaks.

var test = false; var success = 'Yay! You Passed!'; var fail = 'Awww! Try Harder!'; if (test) { console.log(success); } else { console.log(fail); };

This will all work on a single line because the end of my statements are signified by their respective semicolons.

Writing JavaScript properly is a balance between giving yourself too much white space & too little.

If Statement Structure / Else If (Optional)

if () {
} else if () {
} else {};

#8

Hi guys!
Im completly lost by now... Previously we learnt about if / else statements. Nobody mentioned that we can replace with word instead of number. When i tried to resolve this excercise i was thinking about return, var and everything. Can anybody help me to put back on track this new way of thinking and help me why should i write my code in a different way.
Here is my code.
Thanks your help in advance

if (0 <= 0.33) {
console.log("rock");
}
else if (0.34 <= 0.66) {
comsole.log("paper");
}
else (0.67 <= 1) {
console.log("scissors");
};