Last active
December 12, 2023 23:04
-
-
Save nemrosim/d3022d9314ec595802305576ce8d283d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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