Last active
June 27, 2020 07:43
-
-
Save yue4u/8cd40e5576e7758c269ff6b8621ad4bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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