javascript學習筆記:隨機數統計

來源:互聯網
上載者:User
 1<div id="div1"></div>
 2<div id="div2"></div>
 3
 4<script>
 5function write1(){
 6    var i = 0;
 7    for(i=0;i<10;i++){
 8        document.getElementById("div1").innerHTML += Math.ceil(Math.random()*10)+";";
 9        }
10    
11   // setTimeout("write1()",100);
12}
13
14function count1(){
15    var testArr = document.getElementById("div1").innerHTML.split(";")
16    var arrLth = testArr.length;
17    var textStr="";
18    var count1=0,count2=0,count3=0,count4=0,count5=0,count6=0,count7=0,count8=0,count9=0,count10=0;
19    for(i=0;i<=arrLth-1;i++){
20        switch(eval(testArr[i])){
21        case 1:count1+=1;
22               break;
23        case 2:count2+=1;
24               break;
25        case 3:count3+=1;
26               break;
27        case 4:count4+=1;
28               break;
29        case 5:count5+=1;
30               break;
31        case 6:count6+=1;
32               break;
33        case 7:count7+=1;
34               break;
35        case 8:count8+=1;
36               break;
37        case 9:count9+=1;
38               break;
39        case 10:count10+=1;
40               break;
41        }
42       
43    }
44     
45   
46    for(i=1;i<=10;i++){
47       textStr+=  "count"+i+"的值為:"+eval("count"+i)+"</br>";
48    }
49    document.getElementById("div2").innerHTML =textStr;
50
51}
52</script>
53
54
55<input id="Button1" type="button" value="button" onclick="write1()" />
56<input id="Button2" type="button" value="統計" onclick="count1()" />
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.