从对象数组内的 3 个数字计算平均数

时间:2021-03-23 19:09:35

标签: javascript node.js arrays function

我在学校有一项作业,要求我们创建一个函数,使用数组中的现有对象计算 3 名学生的平均分数。我有下面的数组和函数,对象推送到空数组(也是赋值的一部分)。

最后一个小时我一直在阅读并尝试不同的方法来编写函数,但我似乎无法将它放在一起以添加当前标记并将它们除以数组长度。

谁能告诉我我错过了什么以及我哪里出错了?

let classList = [];

const enrollStudent = (firstName, lastName, age, currentMark) => {
      newStudent = {firstName, lastName, age, currentMark};
      classList.push(newStudent);
      console.log(classList);
  };

  addNewStudent("Mark", "Smith", 24, 85);
  addNewStudent("Dane", "Joe", 21, 90);
  addNewStudent("Steven", "McKnight", 23, 54);

const getClassAverage = arr => classList.reduce((newStudent.currentMark) => currentMark + currentMark, 0) / classList.length

1 个答案:

答案 0 :(得分:0)

let classList = [];

const enrollStudent = (firstName, lastName, age, currentMark) => {
      newStudent = {firstName, lastName, age, currentMark};
      classList.push(newStudent);
      console.log(classList);
  };

  enrollStudent("Mark", "Smith", 24, 85);
  enrollStudent("Dane", "Joe", 21, 90);
  enrollStudent("Steven", "McKnight", 23, 54);

  const total = classList.reduce( (acc, student) => { 
            return acc + student.currentMark
}, 0)


const getClassAverage = total / classList.length // 76.33333333333333
相关问题