侧边栏壁纸
  • 累计撰写 197 篇文章
  • 累计收到 496 条评论

Generator抽奖的实现

2019-11-10 / 0 评论 / 37 阅读
function *draw(first = 1, two = 2, three = 3) {
    let one_arr = ['a1', 'a2', 'a3', 'a4']
    let two_arr = ['b1', 'b2', 'b3', 'b4']
    let three_arr = ['c1', 'c2', 'c3', 'c4']
    let judge = []
    for (let i = 0; i< first; i++) {
        let random = Math.floor(Math.random() * one_arr.length)
        judge = judge.concat(one_arr.splice(random, 1))
    }
    for (let i = 0; i< two; i++) {
        let random = Math.floor(Math.random() * two_arr.length)
        judge = judge.concat(two_arr.splice(random, 1))
    }
    for (let i = 0; i< three; i++) {
        let random = Math.floor(Math.random() * three.lenght)
        judge = judge.concat(three_arr.splice(random, 1))
    }
    for (let i of judge) {
        yield console.log(i)
    }
}

let d = draw()
d.next()

评论一下?

OωO
取消