原生js 写一个随机抽奖

要求:总共10个奖项,一等奖一个,二等奖2个,三等奖3个依次类推,抽中后,相应的奖项去除

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id='randomNum'></div>
    <div id="buttons" style="border: 1px solid #eee; padding: 6px; width: 50px; height: 16px; border-radius: 10px; display: flex; justify-content: center; align-items: center;">抽奖</div>
</body>
<script>
    let dataList = [
        {
            id: 0,
            type: 1,
            title: '一等奖'
        },
        {
            id: 1,
            type: 2,
            title: '二等奖'
        },
        {
            id: 2,
            type: 3,
            title: '三等奖'
        },
        {
            id: 3,
            type: 4,
            title: '四等奖'
        },
        {
            id: 4,
            type: 5,
            title: '五等奖'
        },
        {
            id: 5,
            type: 6,
            title: '六等奖'
        },
        {
            id: 6,
            type: 7,
            title: '七等奖'
        },
        {
            id: 7,
            type: 8,
            title: '八等奖'
        },
        {
            id: 8,
            type: 9,
            title: '九等奖'
        },
        {
            id: 9,
            type: 10,
            title: '安慰奖'
        }
    ]


    let selectData = {}
    let ranData = []
    let sum = 0
    let ranSums = 0
    window.onload = function () {
        initData()
    }
    initData = function () {
        sum = 0
        ranSums = 0
        if (dataList.length) {
            if (selectData.length) {
                let selectDone = dataList.findIndex(items => item.id == selectData.id)
                    dataList[selectDone].type = dataList[selectDone].type - 1
                    if (dataList[selectDone].type == 0) {
                        dataList.splice(selectDone, 1)
                    }
                    selectData = {}
            }
            dataList.forEach((item) => {
                ranSums = ranSums + item.type
            })
            dataList.map((item, index) => {
                sum = sum + item.type
                item.randMaxNum = sum
                if (index == 0) {
                    item.randMinNum = item.type
                } else {
                    item.randMinNum = dataList[index - 1].randMaxNum + 1
                }
            })
        }else{
            let randomText = document.getElementById('randomNum')
            randomText.innerHTML = '奖项已全部抽完'
        }

    }
    document.getElementById("buttons").onclick = function () {
        raffleAlgorithm()
    };
    raffleAlgorithm = function () {
        let random = null
        random = Math.floor(Math.random() * ranSums) + 1
        dataList.forEach((item, index) => {
            console.log(item)
            if (random >= item.randMinNum && random <= item.randMaxNum) {
                console.log(item, 'item--', item.title)
                selectData = item
                let randomText = document.getElementById('randomNum')
                randomText.innerHTML = item.title
                initData()
            }
        })

    }
</script>

</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值