Rock, Paper, Scissors, Lizard, Spock
by afrievalt
JavaScript
var options = ["paper", "rock", "lizard", "spock", "scissors"],
result = [" ties ", " beats ", " loses to "],
i = Math.floor(Math.random() * 5),
randomChoice = options[i],
bigBang = function(choice1, choice2) {
var index1 = options.indexOf(choice1),
index2 = options.indexOf(choice2),
dif = index2 - index1;
if(dif < 0) {
dif += options.length;
}
while(dif > 2) {
dif -= 2;
}
return choice1 + result[dif] + choice2;
};
alert(bigBang(randomChoice, "rock"));
alert(bigBang("spock", "scissors"));
alert(bigBang("spock", "paper"));
alert(bigBang("spock", "rock"));
alert(bigBang("spock", "lizard"));
alert(bigBang("spock", "spock"));