【问题标题】:Calculate average number from 3 numbers inside of an object array从对象数组中的 3 个数字计算平均数
【发布时间】:2021-03-23 19:09:35
【问题描述】:

我在学校有一项作业,要求我们创建一个函数,使用数组中的现有对象计算 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

【问题讨论】:

    标签: javascript node.js arrays function


    【解决方案1】:
    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
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多