javascript-生成并打印所有学生的总平均最终成绩

时间:2019-09-13 11:43:42

标签: javascript arrays

因此,我尝试生成和打印所有学生的总平均最终成绩,其权重为:作业= 40%,教程= 10%,考试= 50%。我一直遇到不同的错误,说我丢失了括号之类的东西,我也觉得我做得不对。我想知道是否有人可以将我推向正确的方向?

let students = [{
      fname: "Jane",
      lname: "Brazier",
      snum: "100366942",
      agrade: 67.59127376966494,
      tgrade: 64.86530868914188,
      egrade: 70.52944558104066
    }, {
      fname: "Ricardo",
      lname: "Allen",
      snum: "100345641",
      agrade: 65.80370345301014,
      tgrade: 75.40211705841241,
      egrade: 55.39348896202821
    }, {
      fname: "Mary",
      lname: "Hernandez",
      snum: "100221207",
      agrade: 71.20761408935981,
      tgrade: 71.37529197926764,
      egrade: 75.82038980457698
    }, {
      fname: "James",
      lname: "Johnson",
      snum: "100200842",
      agrade: 72.5791318299902,
      tgrade: 81.65883679807183,
      egrade: 85.19664228946989
    }, {
      fname: "Gene",
      lname: "Jeanlouis",
      snum: "100341666",
      agrade: 74.16481515505846,
      tgrade: 68.20592386917109,
      egrade: 78.25975050135006
    }];


    var examAverage = students.reduce(function (accumulator, student) {
	    return accumulator + student.egrade;
    }, 0);

    var assignmentAverage = students.reduce(function (accumulator, student) {
	    return accumulator + student.agrade;
    }, 0);

    var tutorialAverage = students.reduce(function (accumulator, student) {
	    return accumulator + student.tgrade;
    }, 0);

    var totalAverage = ((examAverage*0.5)+(assignmentAverage*0.4)+(tutorialAverage*0.1));
    console.log(totalAverage);

更新:因此它当前打印出359,这显然太高了。我不确定为什么要这么做。它应该在0-100之间打印出平均值

3 个答案:

答案 0 :(得分:0)

您缺少)来关闭您的功能。现在,它就像一种魅力一样工作,请检查以下内容:

var students = [{
  fname: "Jane",
  lname: "Brazier",
  snum: "100366942",
  agrade: 67.59127376966494,
  tgrade: 64.86530868914188,
  egrade: 70.52944558104066
}, {
  fname: "Ricardo",
  lname: "Allen",
  snum: "100345641",
  agrade: 65.80370345301014,
  tgrade: 75.40211705841241,
  egrade: 55.39348896202821
}, {
  fname: "Mary",
  lname: "Hernandez",
  snum: "100221207",
  agrade: 71.20761408935981,
  tgrade: 71.37529197926764,
  egrade: 75.82038980457698
}, {
  fname: "James",
  lname: "Johnson",
  snum: "100200842",
  agrade: 72.5791318299902,
  tgrade: 81.65883679807183,
  egrade: 85.19664228946989
}, {
  fname: "Gene",
  lname: "Jeanlouis",
  snum: "100341666",
  agrade: 74.16481515505846,
  tgrade: 68.20592386917109,
  egrade: 78.25975050135006
}];


var examAverage = students.reduce(function (accumulator, student) {
    return accumulator + student.egrade;
}, 0);

var assignmentAverage = students.reduce(function (accumulator, student) {
    return accumulator + student.agrade;
}, 0);

var tutorialAverage = students.reduce(function (accumulator, student) {
    return accumulator + student.tgrade;
}, 0);

var totalAverage = ((examAverage*0.5)+(assignmentAverage*0.4)+(tutorialAverage*0.1));

console.log(totalAverage);

答案 1 :(得分:0)

您没有计算 time_t rawtime; struct tm * timeinfo; time( &rawtime ); timeinfo = localtime( &rawtime ); printf("%02d:%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); 的平均值。计算出总和后,必须通过计算学生数组的长度来找出平均值

此外,我建议您不要使用多个egrade, agrade and tgrade。通过一次迭代(.reduce(),您可以轻松计算总和。

.reduce

答案 2 :(得分:0)

我建议使用abstact方法,通过获取一些辅助数组,其中一个用于辅助键和权重。

let students = [{ fname: "Jane", lname: "Brazier", snum: "100366942", agrade: 67.59127376966494, tgrade: 64.86530868914188, egrade: 70.52944558104066 }, { fname: "Ricardo", lname: "Allen", snum: "100345641", agrade: 65.80370345301014, tgrade: 75.40211705841241, egrade: 55.39348896202821 }, { fname: "Mary", lname: "Hernandez", snum: "100221207", agrade: 71.20761408935981, tgrade: 71.37529197926764, egrade: 75.82038980457698 }, { fname: "James", lname: "Johnson", snum: "100200842", agrade: 72.5791318299902, tgrade: 81.65883679807183, egrade: 85.19664228946989 }, { fname: "Gene", lname: "Jeanlouis", snum: "100341666", agrade: 74.16481515505846, tgrade: 68.20592386917109, egrade: 78.25975050135006 }],
    keys = ['agrade', 'tgrade', 'egrade'],
    weights = [0.4, 0.1, 0.5],
    sums = students.reduce(function (accumulator, student) {
        keys.forEach(function (key) {
            accumulator[key] = (accumulator[key] || 0) + student[key];
        });
        return accumulator;
    }, {}),
    totalAverage = weights.reduce(function (sum, weight, index) {
         return sum + sums[keys[index]] / students.length * weight;
    }, 0);

console.log(totalAverage);