Lesson 8 Help (What if choice one is scissors.)


#1


[What I'm Stuck On]


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


#2

Hello @awesomefacexdxd,

Where you are going wrong is with the structure of your if statements! You are putting a semicolon after the if statement... however you are meant to put an opening curly brace.

if (/* Some Condition */) ; // WRONG
if (/* Some Condition */) {
    // Body...
} // CORRECT

Fix up your code... and you shall be fine!


#3

I fixed those and it still gives me the same error /*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"){
}
else {
return "Scissors wins";}

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

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

}


#4

Hello again!

Can you repost your code... but this time can you wrap it around in three backticks

```
//code goes here
```

It will make your code easier to read...


#5

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"){
    } 
    else {
        return "Scissors wins";}
        
  else if (choice1 === "scissors"){
    if (choice2 === "rock"){
    } 
    else {
        return "rock wins";}
        
      else if (choice1 === "scissors"){
    if (choice2 === "paper"){
    } 
    else {
        return "Scissors wins";}
    
}
compare (userChoice, computerChoice);

#6

Hello!

It seems you have two else if() branches.. both of them just checking whether choice 1 is scissors... inside of the else if () branch.. if you erase them and add the relevant else statements... then your code shall work properly


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.