Skip to content

Instantly share code, notes, and snippets.

@clavery
Last active December 21, 2015 23:39
Show Gist options
  • Save clavery/6383688 to your computer and use it in GitHub Desktop.
Save clavery/6383688 to your computer and use it in GitHub Desktop.
lizard spock version (second is faster due to not calling array methods)
function compare(me, comp) {
var plays = ["scissors", "paper", "rock", "lizard", "spock"];
var mine = plays.indexOf(me);
var computer = plays.indexOf(comp);
if (mine === computer)
return "You Tie";
var result = ((mine + 1) % 5) === computer || ((mine + 3) % 5) === computer;
if (result)
return "You Win";
else
return "You Lose";
}
console.log(compare('spock', 'scissors'), 'win');
console.log(compare('scissors', 'paper'), 'win');
console.log(compare('paper', 'scissors'), 'lose');
console.log(compare('rock', 'scissors'), 'win');
console.log(compare('paper', 'spock'), 'win');
console.log(compare('lizard', 'spock'), 'win');
console.log(compare('rock', 'lizard'), 'win');
console.log(compare('rock', 'lizard'), 'win');
console.log(compare('spock', 'paper'), 'lose');
console.log(compare('scissors', 'spock'), 'lose');
console.log(compare('spock', 'paper'), 'lose');
console.log(compare('spock', 'rock'), 'win');
function compare(me, comp) {
var S = "scissors";
var P = "paper";
var R = "rock";
var L = "lizard";
var K = "spock";
var mine = me === S ? 0 : me === P ? 1 : me === R ? 2 : me === L ? 3 : me === K ? 4 : null;
var computer = comp === S ? 0 : comp === P ? 1 : comp === R ? 2 : comp === L ? 3 : comp === K ? 4 : null;
if (mine === computer)
return "You Tie";
var result = ((mine + 1) % 5) === computer || ((mine + 3) % 5) === computer;
if (result)
return "You Win";
else
return "You Lose";
}
console.log(compare('spock', 'scissors'), 'win');
console.log(compare('scissors', 'paper'), 'win');
console.log(compare('paper', 'scissors'), 'lose');
console.log(compare('rock', 'scissors'), 'win');
console.log(compare('paper', 'spock'), 'win');
console.log(compare('lizard', 'spock'), 'win');
console.log(compare('rock', 'lizard'), 'win');
console.log(compare('rock', 'lizard'), 'win');
console.log(compare('spock', 'paper'), 'lose');
console.log(compare('scissors', 'spock'), 'lose');
console.log(compare('spock', 'paper'), 'lose');
console.log(compare('spock', 'rock'), 'win');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment