Skip to content

Instantly share code, notes, and snippets.

@yue4u
Last active June 27, 2020 07:43
Show Gist options
  • Save yue4u/8cd40e5576e7758c269ff6b8621ad4bc to your computer and use it in GitHub Desktop.
Save yue4u/8cd40e5576e7758c269ff6b8621ad4bc to your computer and use it in GitHub Desktop.
const fortuneMap = {
[Symbol("daikichi")]: 5,
[Symbol("chuukichi")]: 10,
[Symbol("syoukichi")]: 20,
[Symbol("kichi")]: 30,
[Symbol("suekichi")]: 20,
[Symbol("kyou")]: 10,
[Symbol("daikyou")]: 5,
};
const randomFortune = () => {
const omikuji = Reflect.ownKeys(fortuneMap).reduce(
({ range, get, ...rest }, key) => ({
key,
range: (range ?? 0) + fortuneMap[key],
*get(num) {
num <= range && get ? yield* get(num) : yield key;
},
...rest,
}),
{
random() {
return this.get(Math.floor(Math.random() * this.range) + 1)
.next()
.value.toString()
.slice(7, -1);
},
}
);
this.target.src = `images/omikuji_${omikuji.random()}.png`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment