JSFiddle - React, Tailwind, and code Playground
by Darby Rathbone
JavaScript
var choices = ['rock', 'paper', 'scissors'];
var roll = function(choice){
var selection = choice||choices.indexOf(choice)+1||Math.ceil(Math.random() * 3);
var comp = function(against){
var winner = chooseWinner(selection,against.valueOf());
if( selection === winner)
if(against.valueOf() === winner)
return "draw";
else
return "win";
else return "lose";
};
comp.valueOf = function(){return selection;};
return comp;
};
var chooseWinner = function (a, b) {
var winner = choices[(Math.abs(a - b) > 1) ? 0 : Math.max(a,b)];
winner.a = choices[a]===winner;winner.b =choices[b]===winner;
return winner;
};
var yours = Math.floor(Math.random() * 3);
var theirs = Math.floor(Math.random() * 3);
console.log(choices[yours] + " vs " + choices[theirs]);
console.dir(chooseWinner(yours,theirs).a);
console.dir(choices);