彩票双色球

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #ball{
            text-align: center;
            line-height: 80px
        }
    .red{
        width: 80px;
        height: 80px;
        border-radius: 100%;
        border: 1px solid wheat;
        background-color: red;
        margin: 10px;
   
    }
    .blue{
        width: 80px;
        height: 80px;
        border-radius: 100%;
        border: 1px solid wheat;
        background-color: blue;
        margin: 10px;
   
    }
    </style>
</head>
<body>
    <div id="ball" style="display:flex">
      
    </div>
   <script>
  
function makeRandom(n, min, max) {
    // 创建一个数组用来保存生成的 n 个随机数
    let nums = [];
    // 控制生成随机数的个数
    for (let i = 0; i < n; i++) {
        // 生成一个范围 min 到 max 的随机数
        let num = Math.round(Math.random() * (max - min) + min);
        // 去重:判断随机数 num 是否存在于 nums 数组里面
        if (nums.indexOf(num) === -1) {
            // 不存在,添加进数组
            nums.push(num);
        } else {
            // 存在,说明当前这一个随机数作废。重新循环一次。
            i--;
        }
    }
    // 把最终的随机数数组返回出去
    return nums;
}

// 生成 6 个 1 到 33 的随机数,结果为数组。
let redBalls = makeRandom(6, 1, 33);
// 生成 1 个 1 到 16 的随机数,结果为数组。
let blueBalls = makeRandom(1, 1, 16);

/* 2. 数字添加到页面上 */

/*
方法1:
makeBalls(): 将数组中的数字生成球,渲染到页面上
    arr: 随机数数组(红球的数组、篮球的数组)
    className: 不同的球有不同 class 属性名
*/
function allball(arr, className) {
    // 依次取出数组中的每一个元素
    for (let i = 0; i < arr.length; i++) {
        // 将取出来的每一个元素放到 <div> 标签中并渲染到页面上
        ball.innerHTML += `<div class="${className}">${arr[i]}</div>`;
    }
}
// 渲染红球
allball(redBalls, "red");
// 渲染蓝球
allball(blueBalls, "blue");

   </script>
</body>
</html>
posted @ 2019-07-11 11:26  阿康呀  阅读(68)  评论(0编辑  收藏  举报