Skip to content

Instantly share code, notes, and snippets.

@davidmfoley
Created October 16, 2018 22:45
Show Gist options
  • Save davidmfoley/d142120b6b018d76a6167962b19935e8 to your computer and use it in GitHub Desktop.
Save davidmfoley/d142120b6b018d76a6167962b19935e8 to your computer and use it in GitHub Desktop.
RPS in JS
const precedence = ['rock', 'scissors', 'paper'];
const results=['TIE', 'FIRST_PLAYER', 'SECOND_PLAYER'];
const ordinal = x => precedence.indexOf(x);
const circularDirection = (a, b) => (ordinal(b) - ordinal(a) + 3) % 3;
const rps = (a, b) => results[circularDirection(a, b)];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment