Please help 7. What if choice1 is paper?


#1

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

what is wrong with this? Please help I have been stuck on this for a while.


#2

You'll have to take off the semi-colon " ; ". off the last closing bracket " } ".


#3

else if(choice1 === "paper") {
if(choice2 === "rock") {
return("paper wins")
}
else {
return("scissors wins")
}
}
That doesn't work either. Any other things?


#4

it says SyntaxError: Unexpected token else


#5

I'm having the same unexpected token else problem - would love some help!


#6

I'm new to this but I believe I was having the same problem and I fixed it by making sure my else if was inside the compare function above it. Delete the closing bracket for your compare function and add a closing bracket to the bottom after return "(scissors wins)"


#7

That's the way:
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")
}
}
}


#8

what is my mistake here?

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("scissor wins")
        }
    }
}

#9

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

as you can see you need to be careful with your {} because currently you have an else if following an else.


#10

thank you for the replay.

but i still dont get, what is the problem...

I have 2 so how should i do it?


#11

You need to pay attention where you if else start and end and what should be nested.


#12

sorry. i am completely lost!


#13

The idea to compare the user and computer choice is to group them:

if tie 
   tie message
else if you choose rock{
    if computer choose paper{
       com wins
    }
    else{ 
       you win
    }
}
else if you choose paper{
    if computer choose rock{
       you wins
    }
    else{ 
       com win
    }
}
else if you choose scissors{
    if computer choose rock{
       com wins
    }
    else{ 
       you win
    }
}

So you have 4 cases tie/rock/paper/scissors and each of them, except tie, has 2 options for what the computer has chosen.


#14

Wrote everything again I just can see the problem!! found that i was missing one }.. but i can see any problem!

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"scissor wins");
        }
    }
};

#15

The structure seems to be much better and the } problem seems to be fixed. The problem here is that your else ifs should have the same target:

else if(choice2 === "scissors") {
else if(choice1 === "paper")

But here one targets choice2 and the other one is targeting choice1. So as choice1 is the userChoice it is probably better to use choice1 there and fix the nested cases accordingly.


#16

Thank you! :smiley:

Its FIXED!! :smiley: :smiley:


#17

Hope this helps:

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

compare(userChoice, computerChoice);