I have a task to calculate the sum and average of integer values from an Array object. Here is the input array:
Input: [
{"string": "John", "integer": 7},
{"string": "Margot", "integer": 8},
{"string": "Jules", "integer": 4},
{"string": "Marco", "integer": 19}
]
Output: 9.5
The issue I am facing is that even though I have created a function to calculate the average, it does not seem to be working correctly. Can someone help me debug this? The code snippet is provided below:
function calculateAverage(student_grades) {
sum = 0;
for(i = 0; i < student_grades.length; i++){
sum += student_grades[i].integer;
}
return sum / student_grades.length;
};
var all_grades = [
{"string": "John", "integer": 7},
{"string": "Margot", "integer": 8},
{"string": "Jules", "integer": 4},
{"string": "Marco", "integer": 19}
]
console.log(calculateAverage(all_grades));