Skip to content

Instantly share code, notes, and snippets.

@nemrosim
Last active December 12, 2023 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nemrosim/d3022d9314ec595802305576ce8d283d to your computer and use it in GitHub Desktop.
Save nemrosim/d3022d9314ec595802305576ce8d283d to your computer and use it in GitHub Desktop.
const students = [
{ name: 'Alice', age: 20, grade: 4.5 },
{ name: 'Bob', age: 21, grade: 3.9 },
{ name: 'Charlie', age: 19, grade: 4.8 },
];
const calculateAverageGrade = (studentArgument) => {
let result = 0;
// сумма всех баллов (версия 1)
studentArgument.forEach((student) => {
result += student.grade;
});
// Можно суммировать еще и так (вариант 2)
const avgV2 = studentArgument.reduce((a, b) => a + b.grade, 0);
// среднее значение
const average = result / studentArgument.length;
// Округление значения 4.3999999999999995
return Math.round(average * 100) / 100;
};
console.log(calculateAverageGrade(students));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment