Computers Choice - says my syntax is incorrect but don't know why?


#1

Hi,

This says my syntax is off - gives me the message 'Unexpected identifier {'
I'm not too sure why - can you help?

I've messed around with the indenting a bit but makes no odds?

thanks!

var computerChoice = Math.random();
    console.log(computerChoice)
    
    if (computerChoice <=0.33) {
        computerChoicde = "rock";}  
    else if (computerChoice > 0.33, computerChoice <= 0.66) {
            computerChoice = "paper";}  
    else (computerChoice >= 0.67) {
            computerChoice ="scissors";
    };

#2

change computerChoicde to computerChoice


#3

I just saw that afterwards - doi! It's corrected now but STILL not working :cry:

The error is still 'unexpected token { '

Code is now:

var computerChoice = Math.random();
    console.log(computerChoice)
    
    if(computerChoice <=0.33) {
        computerChoice = "rock";
    } else if(computerChoice > 0.33, computerChoice <= 0.66 {
        computerChoice = "paper";
    } else(computerChoice >= 0.67) {
        computerChoice ="scissors";
    }

#4

the same story... what's wrong? :sweat:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log (computerChoice);
if (computerChoice<=0.33) {computerChoice="rock";} else if (computerChoice>0.33,computerChoice<=0.66) {computerChoice="paper";} else (computerChoice>=0.67) {computerChoice="scissors";};


#5

Not sure, its so painful. I've been thinking though that we don't need any condition for the final 'else' because this is used if the other two conditions are not true. Its like say 'otherwise do this'
could just have:
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log (computerChoice);
if (computerChoice<=0.33) {computerChoice="rock";} else if (computerChoice>0.33,computerChoice<=0.66) {computerChoice="paper";} else {computerChoice="scissors";};

Anyway when/if I've cracked it i'll let you know.


#6

your else if statement's test conditions need to wrapped up with both ( and ).

also to test for two conditions you need to include &&
(computerChoice > 0.33 && computerChoice <= 0.66)

else doesn't need a test condition bracket.
it would run, if the initial test conditions return false
else {
computerChoice ="scissors";
}


#7

I found out!

if (condition1) {
code code code;
} else if (condition2) {
code code code;
} else {
code code code;
}

there is NO (condition) in the third line :blush: )


#8

Hi thingsofyourmind,

i dont understand what you mean by this exactly - can you elaborate?

thanks for your help!


#9

It also works with the ","

(0.34 <= computerChoice, computerChoice <= 0.66)

I agree that "&&"is better, but why does the "," works too?


#10

oh, i was going off your code from earlier


#11

Eurika! Thats exactly what I've missed - thanks so much!

Final code that worked was:

var computerChoice = Math.random();
console.log(computerChoice)

if(computerChoice <= 0.33) {
    computerChoice = "rock";
} else if(computerChoice <= 0.66) {
    computerChoice = "paper";
} else {
    computerChoice ="scissors";
}

console.log(computerChoice);

#12

from some testing, it doesn't even take into account the test condition before the comma
(0.34 <= computerChoice, computerChoice <= 0.66)

the 0.34 <= computerChoice doesn't even factor into the test condition. im surprised it still works with a , in there.

it picks up the paper2 in the 3rd test, but the 2nd test it doesn't,which should be the proper syntax

Imgur