简体   繁体   English

为HTML表格中的单元格分配值

[英]Assign a value to cell in a HTML table

I have an array of random numbers and want to count the number of times each number appears in the array. 我有一个随机数数组,想计算每个数字出现在数组中的次数。

I have the following code but am unable to figure out how to assign the count values in the html table. 我有以下代码,但无法弄清楚如何在html表中分配计数值。

JavaScript: JavaScript:

<script>
    var arr = [];

    function getRandom(num) {
        return Math.round(Math.random() * num) + 1;
    }

    for (var i = 0; i < 1000; i++) {
        arr.push(getRandom(30));

    }

    var count1 = 0;
    var count2 = 0;
    var count3 = 0;
    var count4 = 0;
    var count5 = 0;

    for (var i = 0; i < 1000; i++) {
        if (arr[i] == 1) {
            count1++;
        }
    }
    for (var i = 0; i < 1000; i++) {
        if (arr[i] == 2) {
            count2++;
        }
    }
    for (var i = 0; i < 1000; i++) {
        if (arr[i] == 3) {
            count3++;
        }
    }
    for (var i = 0; i < 1000; i++) {
        if (arr[i] == 4) {
            count4++;
        }
    }
    for (var i = 0; i < 1000; i++) {
        if (arr[i] == 5) {
            count5++;
        }
    }

    }
</script>

HTML: HTML:

<table border="1">
    <tr>
        <td>Number</td>
        <td>Frequency</td>
    </tr>
    <tr>
        <td>1</td>
        <td id="count1" </td>
    </tr>
    <tr>
        <td>2</td>
        <td id="count2"></td>
    </tr>
    <tr>
        <td>3</td>
        <td id="count3"></td>
    </tr>
    <tr>
        <td>4</td>
        <td id="count4"></td>
    </tr>
    <tr>
        <td>5</td>
        <td id="count5"></td>
    </tr>
    <tr>
</table>

Use the below code 使用下面的代码

function f(){
var arr = [];

    function getRandom( num ){
       return Math.round(Math.random() * num)+1;
    }
   for (var i = 0; i < 1000; i++) {
       arr.push(getRandom( 30 ));    
   }
 var count1=0;
 var count2=0;
 var count3=0;
 var count4=0;
 var count5=0;


  for (i=0;i<1000;i++){
    if(arr[i]==1){
      count1++;
      }

     if(arr[i]==2){
     count2++;
      }

       if(arr[i]==3){
         count3++;
    }
      if(arr[i]==4){
        count4++;
    }
      if(arr[i]==5){
       count5++;
      }
  }

document.getElementById("count1").innerHTML=count1;
document.getElementById("count2").innerHTML=count2;
document.getElementById("count3").innerHTML=count3;
document.getElementById("count4").innerHTML=count4;
document.getElementById("count5").innerHTML=count5;
}

Demo 演示版

It would be better (simpler and quicker) to use a second array to count the frequencies, at the same time as you assign the random numbers, then check this array and put the values into the table cells. 在分配随机数的同时,使用第二个数组对频率进行计数会更好(更简单,更快),然后检查该数组并将值放入表格单元格中。 Also, easier to identify the table cells by a class rather than giving them all similar id s. 而且,比通过class来标识表单元更容易,而不是给它们所有相似的id ( jsfiddle ) jsfiddle

var arr = [], counts = [],
    countCells = document.getElementsByClassName('count');

function getRandom(num) {
    return Math.round(Math.random() * num) + 1;
}
for (var i = 0; i < 1000; i++) {
    var r = getRandom(30);
    arr.push(r);
    counts[r] = (counts[r] || 0) + 1;
}
for (var i = 0; i < countCells.length; i++) {
    countCells[i].innerHTML = counts[i + 1] || 0;
}

Not the issue but you could simplify the process and get a count for all the values : 不是问题,但是您可以简化过程并计算所有值:

var arr    = [],
    totals = [];

function getRandom(num) {
    return Math.round(Math.random() * num) + 1;
}

for(var i=0; i<1000; i++) {
    var val   = getRandom(30),
        count = totals[val] || 0;

    arr.push(val);
    totals[val] = ++count;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM