用js实现双色球

通过arguments.call()实现对原函数的递归调用;

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 随机取1-33之间的6个数字,不可重复,并且按由小到大排序。随机取1-16之间的两个数字,和之前的6个数字组成数组 -->
    <input type="button" value="点击生成双色球数组" id="btn">
    <p id="numArr"></p>
    <script>
        function getNumArr() {
            var arr_red = [];

            function getRandomNum_red() {
                var randomNumRed = Math.floor(Math.random() * 33) + 1;
                if (arr_red.indexOf(randomNumRed) == -1) {
                    arr_red.push(randomNumRed);
                } else {
                    arguments.callee();
                }
            }
            for (let i = 0; i < 6; i++) {
                getRandomNum_red();
            }
            console.log("红色球:" + arr_red);
            var arr_blue = [];

            function getRandomNum_blue() {
                var randomNumBlue = Math.floor(Math.random() * 16) + 1;
                if (arr_blue.indexOf(randomNumBlue) == -1) {
                    arr_blue.push(randomNumBlue);
                } else {
                    arguments.callee();
                }
            }
            for (let i = 0; i < 2; i++) {
                getRandomNum_blue();
            }
            console.log("蓝色球" + arr_blue);
            arr_red = arr_red.sort(function (a, b) {
                return a - b;
            });
            arr_blue = arr_blue.sort(function (a, b) {
                return a - b;
            });
            document.getElementById("numArr").innerHTML = `<span style='color:red;'>${arr_red.toString()},</span><span style='color:blue;'>${arr_blue.toString()}</span>`;
        }
        document.getElementById("btn").addEventListener("click", getNumArr, false);
    </script>
</body>

</html>
posted @ 2020-04-04 20:31  Syinho  阅读(1311)  评论(0编辑  收藏  举报