I am trying to add these integer values from this Array object to get the sum and in turn the average.
Input: [
{"string": "John", "integer": 7},
{"string": "Margot", "integer": 8},
{"string": "Jules", "integer": 4},
{"string": "Marco", "integer": 19}
]
Output: 9.5
I have been able to do this function, however, I am not able to add the values from the integer into a sum variable and divide by the objectName.length. My sample code is beneath, I want to understand why it is not working?
function my_average_mark(student_grades) {
sum = 0;
for( i = 0; i < student_grades.length; i++){
// console.log(student_grades[i].integer);
sum += student_grades[i].integer;
console.log(sum);
}
};
all_grades = [
{"string": "John", "integer": 7},
{"string": "Margot", "integer": 8},
{"string": "Jules", "integer": 4},
{"string": "Marco", "integer": 19}
]
console.log(my_average_mark(all_grades));
Inputandall_gradesare arrays of objects...console.log(Input.reduce((n, {integer}) => n + integer, 0)/Input.length)