sntaxError: Unexpected token else


#1

Continuing the discussion from 7. Can't seem to figure out what is the matter with my code :/:


  1. WHat if choice1 is paper?

    SyntaxError: Unexpected token else


/*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 {
                return "scissors wins";}
}

};

console.log (compare);


#2

Hi you didn't close the else if statement here

....
else if (choice1 === "rock") { 
    if (choice2 === "scissors") { 
        return "rock wins"; 
    } 
    else { 
        return "paper wins"; 
    }
} #it was missing that one
.....

#3

hm. if i add there, i get new error: "SyntaxError: Unexpected token }"


#4

then here you closed the else if statement earlier..

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

#5

must have screwed something up, still getting 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";
    }

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

    else {
        return "scissor wins";}
        } 
    }

};
console.log(compare);


#6

your code should be like that

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie"
    } 
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {#here add one { 
            return "rock wins";
        }    
        else {
            return "paper wins";
        }
    } 
    else if (choice1 === "paper") {
       if (choice2 === "rock") {
           return "paper wins";
       }  
       else {
           return "scissor wins";
       }
    }
}
}#remove this one

#7

ok just did exactly that. added one where you indicated and removed the one near bottom. this yields:
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";
}
}
else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
}
else {
return "scissor wins";
}
}
};

console.log(compare)


#8

nm got it by adding another one

ar 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";
}
}
};

console.log(compare)