Unexpected token-else



I just want to know what I am doing wrong here. I keep getting a syntax error response from the console saying "Unexpected token: else". Can anyone help me? Thanks.

Replace this line with your code. 
 var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random()
    if ( computerChoice >= 0)
    (computerChoice <= 0.33)
        computerChoice = "rock"
    else if (computerChoice >= 0.34)
    (computerChoice <= 0.66)
        computerChoice = "paper"
    else (computerChoice >= 0.67)
    (computerChoice <= 1)
        computerChoice = "scissors"


Starting from the lowest and working upwards as you are doing does not need any greater than conditionals, just less than.

if (computerChoice <= 0.33) {

else if (computerChoice < 0.67) {

} else {


Consider that we are working here with intervals, the span of which ranges from 0 to 1, not including 1 itself.

interval =>  { random() such that 0 <= random() < 1; random() is a float }

Being as the interval has infinite values, we can divide it by any number. Every positive fraction less than 1 fits into this interval. In our case (this exercise) there are three intervals,

{0 <= x < 1/3}, {1/3 <= x < 2/3}, {2/3 <= x < 1}

You see how a number's size determines which interval it falls into? If we change the order, then we need to test both lower and upper bounds, but if we keep in this order, we only need to test upper bounds. And, the good part is we don't have to test at all in the last interval. It is the default if the first two tests fail.

    if (c < 1/3) {
        // lowest interval
    else if (c < 2/3) {
        // mid-range interval
    } else {
        // highest interval


Thanks so much! One question though. Why would that affect a token? I mean
the if else statement and the math.random terms are just playing with
values, but syntax said that the token "else" was misplaced.


Your code contained so many errors I felt it best to illustrate with valid JS. Compare to yours and see it you can spot where the errors are.


Just did thanks for the tip, but I think what my initial question was more
of how would a token be affected by an if/else statement. And sorry that my
code is very bad, I just started learning JavaScript about a few weeks ago.


When there is an error or unexpected code before an else, that code may not raise an error but the placement of else will be show stopper. Because your code has two conditional expressions, it breaks the if statement. Consequently, else is not exprected by the interpreter.


if (condition) {
    // a code block delimited with { and }
else if (condition2) {
    // another code block delimited with { and }
else {
    // a final code block delimited with { and }

Note that if only takes one (expression) which is followed by one {block}. else never takes a condtional expression. It is the default code block (if required).


Thanks dude, makes sense.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.