对for循环中的数字进行分类和计数 - Javascript

时间:2015-02-19 06:29:43

标签: javascript arrays sorting

我第一次写Javascript而且卡住了。对于该程序,我应该将得分分类为A = 90-100,B = 80-89,C = 70-79,D = 60-69和F <60。然后打印出有x A,x B,x C,x D和x F的信息。其中x是属于该类别的分数。我该怎么做呢?

</body>
</html>

<!DOCTYPE html>
<head>
</head>
<body>
<script type = "text/Javascript"> 

      function testScores(){
      scores = ["65", "75", "99", "82", "77", "100", "75", "88", "100", "75" ];

      }
      function sortScores(){
      for(i=1; i<=9; i = i+1)
         document.write(scores[i] + " "); 

      }

</script>
<button type="button" onclick = "testScores();sortScores()"> Test Scores </button>

</body>
</html>

3 个答案:

答案 0 :(得分:0)

循环遍历数组(注意,它们是数字,因此您不需要"),并且每次数字与过滤器匹配时增加变量 - 然后打印出变量:

&#13;
&#13;
var a = 0;
    var b = 0;
    var c = 0;
    var d = 0;
    var f = 0;
    var arr = [65, 75, 99, 82, 77, 100, 75, 88, 100, 75];
    for (var i = 0; i < arr.length; i++) {
      if (89 < arr[i] && arr[i] <= 100) {
        a++;
      }
      if (79 < arr[i] && arr[i] <= 90) {
        b++;
      }
      if (69 < arr[i] && arr[i] <= 80) {
        c++;
      }
      if (59 < arr[i] && arr[i] <= 70) {
        d++;
      }
      if (arr[i] <= 59) {
        f++;
      }
    }


    document.getElementById('results').innerHTML=('A = ' + a + '<br>B = ' + b + '<br>C = ' + c + '<br>D = ' + d + '<br>F = ' + f + '<br>');
&#13;
<div id="results"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试一下

  • 您应该使用two-dimensional-array,以便日后进行更改。这意味着如果你将来有一个新的等级,你必须添加范围和等级名称,如(a,b,c,ETC。)

View DEMO

&#13;
&#13;
function testScores() {
            try {
                    var fullNameArray = {
                        0: [90, 100, 0,"A"],
                        1: [80, 89, 0,"B"],
                        2: [70, 79, 0,"C"],
                        3: [60, 69, 0,"D"],
                        4: [0, 59, 0,"F"]
                    }
                var scores = ["65", "75", "99", "82", "77", "100", "75", "88", "100", "75"];
                var len = numProps(fullNameArray);
                for (var i = 0; i <= scores.length; i = i + 1) {

                    for ( var k = 0; k <= len; k++) {
                        if (scores[i] >= fullNameArray[k][0] && scores[i] <= fullNameArray[k][1]) {
                            
                            fullNameArray[k][2] = fullNameArray[k][2] + 1;
                        }
                    }

                }
                for (var D = 0; D <= len; D++) {                    
                    document.getElementById('diVID').innerHTML = document.getElementById('diVID').innerHTML+"<br/>"+fullNameArray[D][3] +" : "+ fullNameArray[D][2];
                    
                }
            }
            catch (err) {
                alert(err);
            }
    }

function numProps(obj) {
  var c = 0;
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) ++c;
  }
  return c-1;
}
&#13;
<input type="button"  value="Click OK" onclick="testScores()" />

<div id="diVID"></div>
&#13;
&#13;
&#13;

<强> JSFIDDLE Here

答案 2 :(得分:0)

这是一个可能的解决方案:

function testScores(){
    var scores = [65, 75, 99, 82, 77, 100, 75, 88, 100, 75];
    var boundaries = [90, 80, 70, 60];
    var points = sortScores(scores, boundaries);
    document.write(" >= " + (boundaries[0]) + ": " + points[0]);
    for (var i=1; i < boundaries.length; ++i) {
        document.write(", " + (boundaries[i-1]-1) + "-" + boundaries[i] + ": " + points[i]);
    }
    document.write(", < " + boundaries[boundaries.length-1] + ": " + points[boundaries.length]);
}

function sortScores(scores, boundaries) { 
    var points = [], group;
    for (group=0; group<=boundaries.length; ++group) {
        points[group] = 0;
    }
    for(i=0; i<scores.length; ++i) {
        group = 0;
        while (group < boundaries.length && scores[i] < boundaries[group]) {
            ++group;
        }
        ++points[group];
    }
    return points;
}

testScores();

在这里小提琴:http://jsfiddle.net/robbyn/72ote3pm/