Syntax Error


#1
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!";
    }
     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"
            }
        }
    else (choice1 === "scissors"){
        if (choice2 === "rock"){
            return "rock wins"
        }
        else {
            return "scissors wins"
        }
    }

console.log compare(computerChoice, userChoice);

Receiving Syntax Error, can anyone help me out?


#2

Hey @endurance506,

I have already said about it here:

:smile:

Get it? :smiley:


#3

Thank! I've fixed that, but still receiving syntax error. unexpected token {


#4

I believe you also need another } in the end.

1) One from the compare function
2) One from the else if loop
3) One from the else loop


#5

That's what i thought, would be a simply fix but still receiving same error. I'm not sure where else the issue could be. Thanks for the review


#6

No problem.

Also look at this now:

You have to use else if, remember? Not another if. :smile:


#7

So i see the problem but not sure of the solution, if/else resolves two scenarios, if /else if/ else resolves three scenarios, including the tie scenario i seem to be running into four scenarios. how do i format out one of the scenarios to remain the formatting of if/ else if/else? Does that make sense? Again thanks for the help.


#8

To make four scenarios you can do it like this:

if/else if/else if/else

:smiley:


#9

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!";
}
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"
}
}
else (choice1 === "scissors"){
if (choice2 === "rock"){
return "rock wins"
}
else {
return "scissors wins"
}
}
}
var result = compare(userChoice,computerChoice);
console.log(result);

completely stuck, not sure why it's not resolved. SyntaxError: unexpected token {


#10

I told you:

This has to be an else if loop.


#11

Thanks for review but still receiving same error. Any thoughts?


#12

Well, else if loop here also, for no requirements should be in an else loop...

Yup, do that all and it does the trick. :smile: