I don't know what is wrong (syntax error, completely lost)


#1

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

#2

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";
} 
} 
};
compare("rock", "scissors");

look this over see what you did wrong Its mostly with the inside if/else statement your missing one it looks like


#3

/*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(choice2 === "scissors"){
        
        return "rock wins";
        
    }else{
        return "paper wins";
        
    }else if(choice1 === "paper"){
        
        if (choice2 === "rock"){
            
         return "paper wins";   
        }else{
            
         return "scissors wins";   
         
        }
        
    }
};

Am having the same issue, it says that one of my else's are unexpected but i don't get it, it seems to look right


#4

Ok this is probably one of the rare times where it is really the same problem :smile:
Is there a reason why this is that common and why you did this?

Anyway the problem lies here:

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

First of all the general idea is that you have a look at the possibilities for choice1 and then nest the posibilities of choice2 inside of these cases. An example for this can be found in your second case which is for mysterious reasons correct.

So the first problem with the above mentioned code is that you look at choice2 first. Then nothing is nested inside but the else is on the same level as the else if and the tie case so you're saying:

"If they do not tie and choice2 is not "scissors" then always "paper wins" "

Sounds like a rigged game to me :smile:
And it causes your problem with the else because the else paper wins statement ends your conditional starting at tie. Meaning your next else if triggers the "unexpected token else"-error because it is not connected to an if because as said the if is ended with the else as this is the default case there are no further cases needed.


#5

lool that allot i relized my mistake.

/*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";
    }
}else if(choice1 === "paper"){
    if(choice2 === "rock"){
     return "paper wins";   
    }else{

    }
}

};


#6

*thanks alot i meant lol


#7

Ah I think it's with the way the directions are posted that is messing everyone up, thanks for the clear up I was really confused.


#8

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

i still dont get it


#9

start from choice1 so like

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{

    }
}
};

#10

Maybe have a look at this post:
https://www.codecademy.com/forum_questions/51fc4178f10c60f4eb001233#answer-54917bd395e378bc1f001cf4
or for the code at what @chiprunner95041 produced.


#11

Lol I feel like an idiot it was so simple thanks man


#12

dont worry about it, remember i made the same mistake lol