JavaScript--机选双色球

 1 <!doctype html>
 2 <html>
 3  <head>
 4   <meta charset="UTF-8">
 5   <title>Document</title>
 6   <script>
 7     function doubleBall(){
 8       var reds=[];//声明空数组reds;
 9       //当reds的个数<6,就要反复生成红球
10       while(reds.length<6){
11         //在1~33之间生成一个随机整数,保存在变量r中
12         var r=Math.floor(Math.random()*33+1);
13         //遍历reds中每个数字
14         for(var i=0;i<reds.length;i++){
15           if(reds[i]==r){//如果当前元素等于r
16             break;//退出循环
17           }
18         }//(遍历结束)
19         if(i==reds.length){//如果i等于length
20           reds.push(r);//将r压入reds中
21         }
22       }//(循环结束)
23       //将reds按数字升序排列
24       reds.sort(function(a,b){return a-b;});
25       //在1~16之间生成一个随机整数保存在变量blue中
26       var blue=Math.floor(Math.random()*16+1);
27       document.write("<b style='color:red'>"+
28         String(reds)
29       +"</b>"+
30       "<b style='color:blue'>"+blue+"</b>");
31     }
32   </script>
33  </head>
34  <body>
35   <button onclick="doubleBall()">机选</button>
36  </body>
37 </html>

当然没有写html和css,所以会比较丑,近期的随笔都是用JavaScript实现一些小的玩意~

原文地址:https://www.cnblogs.com/chenzeyongjsj/p/5495698.html