I wrote a little Javascript rock/paper/scissors/lizard/spock game and when the user clicks the button to make their choice from the five options, I choose the computer's randomly and then compare the two to find a winner and output the result. It works, but I have the feeling that the findWinner
function can be a lot shorter or more elegant. I'm trying to improve my Javascript and jQuery and was hoping for someone to help me with next steps here:.
function findWinner(choiceA,choiceB) {
if (choiceA == choiceB) {
return "It's a tie!";
}
if (choiceA == 'Rock') {
if (choiceB == 'Lizard' || choiceB == 'Scissors') {
return "You win!";
}
else if (choiceB == 'Spock' || choiceB == 'Paper') {
return choiceB;
}
}
else if (choiceA == 'Paper') {
if (choiceB == 'Rock' || choiceB == 'Spock') {
return "You win!";
}
else if (choiceB == 'Scissors' || choiceB == 'Lizard') {
return "Computer wins :( ";
}
}
else if (choiceA == 'Scissors') {
if (choiceB == 'Paper' || choiceB == 'Lizard') {
return "You win!";
}
else if (choiceB == 'Spock' || choiceB == 'Rock') {
return "Computer wins :( ";
}
}
else if (choiceA == 'Lizard') {
if (choiceB == 'Paper' || choiceB == 'Spock') {
return "You win!";
}
else if (choiceB == 'Rock' || choiceB == 'Scissors') {
return "Computer wins :( ";
}
}
else if (choiceA == 'Spock') {
if (choiceB == 'Rock' || choiceB == 'Scissor') {
return "You win!";
}
else if (choiceB == 'Paper' || choiceB == 'Lizard') {
return "Computer wins :( ";
}
}
}
This just seems very repetitive, but I'm not sure where to go next with it.