Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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