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()" />