7. Syntax error


#1

Keep having a syntax error with this code. Says that 'else' is an unexpected token. Does anyone know where I went wrong with this. I keep looking at the code and can't see where I've gone wrong.

/*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";
            }
        
        }
    
      }; 
};

#2

          if(choice2 === "scissors")
                return "rock wins";
                
        
        }else{  //closing else if open else
            return "paper wins";
            
            }else if // closing else unexpected else if

Have a look at the hint of 6/9 on how to build the else if case.


#3

Hi, you messed up with "}"
Check my code below:

/*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";
}
}
};


#4

Does anyone know why we need to start another curly bracket?


#5

What do you mean? Functions, conditions and loops (later) have the code corresponding to them wrapped in {} so if you use a condition in a function you have {} in {}.


#6

do not know whats wrong with this codes

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

syntax error : unexpected end of input


#7

You seem to miss a } at the very end to close the function.