如何按字母等级对测试成绩进行排序

时间:2015-03-25 21:24:36

标签: javascript arrays sorting

所有得分的前20%是A. 第二个20%的分数是B. 第三个20%的分数是C. 第四个20%的分数是D. 最低20%的分数是F。

以下分数:

99, 92, 91, 91, 89, 85, 83, 82, 80, 79, 78, 78, 77, 76, 75, 74, 62, 55, 43,
20

我需要编写一个函数,它将任意长度的得分列表(不一定是上面用作上例的列表)作为参数,并返回与这些得分相关的成绩列表。

到目前为止,我得到的是:

<!DOCTYPE html>
<html>
<body>

<p>Click the button sort out grades.</p>

<button onclick="myFunction()">Calculate</button>

<p id="testScores"></p>
<p id="top"><p>
<p id="second"><p>
<p id="third"><p>
<p id="fourth"><p>
<p id="bottom"><p>

<script>
var grades = [89, 92, 20, 91, 99, 85, 83, 82, 77, 62, 78, 78, 80, 76, 75, 43, 72, 55, 74,
    91];
document.getElementById("testScores").innerHTML = grades;    

function myFunction() {
    grades.sort(function(a, b){return a-b});
    document.getElementById("testScores").innerHTML = grades;
    document.getElementById("top").innerHTML = grades;
    document.getElementById("second").innerHTML = grades;
    document.getElementById("third").innerHTML = grades;
    document.getElementById("fourth").innerHTML = grades;
    document.getElementById("bottom").innerHTML = grades;

}
</script>

</body>
</html>

我能够让它从最大到最小,并且每行都有单独的行(A,B,C,D,F)。我无法弄清楚如何以20%的增量分配成绩。

I want the output to be:
A=99,92,91,91
B=89,85,83,82
C=80,79,78,78
D=77,76,75,74
F=62,55,43,20

2 个答案:

答案 0 :(得分:0)

你可以试试这个,

var grades = [89, 92, 20, 91, 99, 85, 83, 82, 77, 62, 78, 78, 80, 76, 75, 43, 72, 55, 74,44,
    91];
document.getElementById("testScores").innerHTML = grades;    

function myFunction() {
    grades.sort(function(a, b){return b-a});
    document.getElementById("testScores").innerHTML = grades;
    var slice = Math.ceil(grades.length * 0.2), init = 0;
    
    document.getElementById("top").innerHTML = grades.slice(init, init+slice);
    init+=slice;
    document.getElementById("second").innerHTML = grades.slice(init, init+slice);
    init+=slice;
    document.getElementById("third").innerHTML = grades.slice(init, init+slice);
    init+=slice;
    document.getElementById("fourth").innerHTML = grades.slice(init, init+slice);
    init+=slice;
    document.getElementById("bottom").innerHTML = grades.slice(init, init+slice);

}
<!DOCTYPE html>
<html>
<body>

<p>Click the button sort out grades.</p>

<button onclick="myFunction()">Calculate</button>

<p id="testScores"></p>
<p id="top"><p>
<p id="second"><p>
<p id="third"><p>
<p id="fourth"><p>
<p id="bottom"><p>

</body>
</html>

答案 1 :(得分:0)

我这样做的方法是创建一个groupByPercentile函数。这需要两个参数(可以使它成为一个,但这更可重用)并返回一个成绩对象,每个对象包含一个分数数组。

function groupByPercentile (sortedArray, percent) {
  var group = {
    "A": [],
    "B": [],
    "C": [],
    "D": [],
    "F": []
  };

  var percentile = sortedArray.length * percent;

  for (var i = 0; i < sortedArray.length; i++) {
    if (group["A"].length < percentile ) { group["A"].push(sortedArray[i]); continue; };
    if (group["B"].length < percentile ) { group["B"].push(sortedArray[i]); continue; };
    if (group["C"].length < percentile ) { group["C"].push(sortedArray[i]); continue; };
    if (group["D"].length < percentile ) { group["D"].push(sortedArray[i]); continue; };
    if (group["F"].length < percentile ) { group["F"].push(sortedArray[i]); continue; };
  };

  //Here you can return a specific grade to get the array, or the object and deal with it later
  return group["A"];

}