Skip to content

Instantly share code, notes, and snippets.

@jamesseanwright
Last active June 27, 2017 15:46
Show Gist options
  • Save jamesseanwright/6e4c40e16ff62925744b to your computer and use it in GitHub Desktop.
Save jamesseanwright/6e4c40e16ff62925744b to your computer and use it in GitHub Desktop.
A n00b's attempt at JavaScript code golf...
/* uncompressed - 430 bytes
* minified (UglifyJS) - 273 bytes
* FYI - indented using tabs because one character vs 4, yo.
* Append ?ts = 4 to URL to format this correctly.
*/
function p() {
var a, b,
m = Math,
r = m.random,
f = m.floor,
n = {
r: 'rock',
p: 'paper',
s: 'scissors'
},
d = [
{ n: 'r', b: 's', l: 'p' },
{ n: 'p', b: 'r', l: 's' },
{ n: 's', b: 'p', l: 'r' }
],
c = d.length,
w, l;
a = d[f(r() * c)];
while (!b || b === a) b = d[f(r() * c)];
w = a.b === b.n ? a : b;
l = w === a ? b : a;
console.log(n[w.n] + ' beats ' + n[l.n]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment