Skip to content

Instantly share code, notes, and snippets.

@thedrewbisset
Created June 1, 2017 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedrewbisset/288cdb3a19e82c1f1e984390317576b5 to your computer and use it in GitHub Desktop.
Save thedrewbisset/288cdb3a19e82c1f1e984390317576b5 to your computer and use it in GitHub Desktop.
function Yatzy(dice) {
const callDirectory = {
ones: 1,
twos: 2,
threes: 3,
fours: 4,
fives: 5,
sixes: 6
}
const sum = function(dice, num) {
return dice.reduce(function(acc, el) {
return (el == num) ? acc += el : acc
}, 0)
}
const sumHandler = {
get: function(target, name) {
return sum(dice, target[name])
}
}
const sumProxy = new Proxy(callDirectory, sumHandler);
return sumProxy;
}
@thedrewbisset
Copy link
Author

Invocations:

Yatzy([1,1,1,2,5]).ones
=> 3
Yatzy([3,5,4,3,2]).threes
=> 6
Yatzy([6,6,6,6,2]).sixes
=> 24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment