Skip to content

Instantly share code, notes, and snippets.

@nunull
Last active August 29, 2015 14:21
Show Gist options
  • Save nunull/8ae509e69ff084950450 to your computer and use it in GitHub Desktop.
Save nunull/8ae509e69ff084950450 to your computer and use it in GitHub Desktop.
Rock, Paper, Scissors
<!DOCTYPE html>
<html>
<head>
<title>Rock, Paper, Scissors</title>
</head>
<body>
<select id="user-choice">
<option value="0">Rock</option>
<option value="1">Paper</option>
<option value="2">Scissors</option>
</select>
<button id="action-perform-move">Start</button>
<strong id="result"></strong>
<script type="text/javascript">
(function() {
var print = function(str) { el('result').innerText = str; },
el = function(id) { return document.getElementById(id); },
performMove = function(user) {
var computer = Math.floor(Math.random() * 3);
if(user === computer) throw 'Tie';
return computer === (user + 2) % 3;
};
el('action-perform-move').addEventListener('click', function() {
var sel = el('user-choice');
var user = parseInt(sel.options[sel.selectedIndex].value);
try {
var res = performMove(user);
if(res) print('You win.');
else print('You loose.');
} catch(e) { print('The result is a tie.'); }
});
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment