educative.io

Educative

Having Problems solving Data Structures: Objects "averages exercise"

var getAverages = function(students){
var averages = [];
let sum=0;
//write your code here
for (grades in students) {
for (i=0;students[grades].length;i++) {
sum+=i;
let avg = sum/grades.length;
averages.push(avg)
}
}

return averages;
}

this is my code and it doesnt work! what changes should i make?

link to the problem: https://www.educative.io/courses/learn-web-development-from-scratch/7n9N5JMAjgQ

Hi! The input variable students is an array, where each element is an Object. You’ve used the for...in loop on students, which will return the index of each element, i.e. 0,1,2,... rather than access the grades property of every student.

In the next loop, your condition students[grades].length is incorrect as you missed any comparison operators.

The line sum += i is incorrect since it will add the indexes of each grade rather than the actual grade value.

Finally, the average must be calculated after the inner loop finishes running.

You can also look at the solution provided in the challenge for more help!