"SyntaxError: Unexpected token else" — #7 "What if choice1 is paper?"


#1



UGH -_-

"SyntaxError: Unexpected token else"
Well, I am back again. It's the same issue but in a different part of the lesson. I just can't seem to spot what's wrong with it. I am new to coding so frustration is kicking. Help please!

/*var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);*/
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return("The result is a tie!");
}
else if(choice1 === "rock") {
if(choice2 === "scissors")
return "rock wins";
} else {
return "paper wins";
}
};
compare= function(choice1, choice2){
else if(choice1 === "paper"){
    if(choice2 === "rock" {
        return "paper wins";
    } else {
        return "scissors wins";
    }
}
};


#2

Two things:

First of all are you sure you want to do this?

(Notice the comment...)

Second of all... Im going to give you a hint on your problem its here:

^^ There is actually two problems on the above quote.


#3

If you are referring to the missing parenthesis, I just corrected it but it still says "SyntaxError: Unexpected token else".

var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return("The result is a tie!");
}
else if(choice1 === "rock") {
if(choice2 === "scissors")
return "rock wins";
} else {
return "paper wins";
}
};
compare= function(choice1, choice2){
else if(choice1 === "paper"){
if(choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
}
}
};


#4

compare = function(choice1, choice2) {
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    }
};

There are two problems above. I would like you to identify it, so you get proud of your self(since you did it). And it will help you solve future problems to..(Beautfied so you can see error better). Another Hint: Make Sure you have the right syntax.


#5

I honest to god don't know :sob:


#6

Ok your whole thing is kinda off sorry i didnt realize... im going to go through the whole thing with you.


#7

First of all you should have all this assuming you passed the last exercice:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if (computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}
var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    } else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "Rock wins";
        } else {
            return "Paper wins";
        }
    }
}

Well lets go with the instructions:

First it says:

Inside the compare() function under the existing code, write another else if statement where the condition is choice1 === "paper".

Ok thats preety easy(ima zoom into the compare function):

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    } else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "Rock wins";
        } else {
            return "Paper wins";
        }
    } 
    else if(choice1 === "paper"){ //Just added else if here as said in instructions..

    }

}

Ok next instruction:

Inside this else if statement, write an if / else statement. If choice2 === "rock", return "paper wins". Else, return "scissors wins".

Preety easy to... just add if/else statement as you learned before and return statements... So the code results:

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    } else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "Rock wins";
        } else {
            return "Paper wins";
        }
    }
    
    else if(choice1 === "paper"){
      if(choice2 == "rock"){ //Added if/else  statement as said in instruction. and returned exactly what it asked.
         return "paper wins"
      }
      else{
           return "scissors wins"
      }
    }

}

So if you combine them together with last piece you should be golden for the next exercice! More importantly, do you understand what i did?


#8

Oh so I didn't have to restate the compare function?


#9

No you didnt.. you just had to update your current compare function @ninaor9uh


#10

Ok I did revised it but still got a "SyntaxError: Unexpected token else". Did i forget a bracket or semi colon? Or are there ones that shouldn't be there?

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return("The result is a tie!");
}
else if(choice1 === "rock") {
if(choice2 === "scissors")
return "rock wins";
} else {
return "paper wins";
}
};
var compare= function(choice1, choice2) {
else if(choice1 === "paper") {
if(choice2 === "rock") {
return "paper wins";
}
else {
return "scissors wins";
}
}
};


#11

else follows if so you can check what's above each else

Most interpreters would give a line number with errors, you really aren't supposed to have to check each and every one.

When I run it locally I get this:

$ node nina.js 
/tmp/js/nina.js:22
	else if(choice1 === "paper") {
	^^^^
SyntaxError: Unexpected token else

#12

thats the same exact thing you didnt improve..


#13

It says else if in the instructions
Thanks though


#14

I am honest to god trying considering the fact that I have never had prior knowledge of JavaScript.
I am not good at this - my teacher decided to make us do three units without asking her questions because "you won't get a lot of help in college". She claims that doing this will "help us learn to figure problems out on our own". I honestly think that she just doesn't want to teach.


#15

An if-statement has an optional else-part.

The else-part can't exist without the if.

else comes after an if.


#16

Basically when you combine all together you get this(correct anwser):

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if (computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}
var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    } else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "Rock wins";
        } else {
            return "Paper wins";
        }
    }
    
    else if(choice1 === "paper"){
      if(choice2 == "rock"){
         return "paper wins" 
      }
      else{
           return "scissors wins"
      }
    }

}

Try to understand what it means though... dont just copy.


#17

Thank you -- I understand it's just sometimes I can't see the error in the pattern :confused: