I'm new to JavaScript and I tried so many things already. I have to sum all values (grades) for each person inside the function. So when I call the function outside with
console.log(totalNumberOfGrades(// I have to put here any of the three person objects... ));
so the inside of the function can sum the values. I tried it with .reduce
and for
...in loops but I can't figure out how to access the values of the objects property when I use the array allPerson
. Thanks for your help and I continue learning.
var person1 = {
gradeMath: 2,
gradeBio: 3,
gradeEnglish: 1
};
var person2 = {
gradeMath: 1,
gradeBio: 2,
gradeEnglish: 2
};
var person3 = {
gradeMath: 3,
gradeBio: 4,
gradeEnglish: 3
};
var allPerson = [person1, person2, person3];
function totalNumberOfGrades(person) {
// return the sum of grades for each person
}
The result look like this:
console.log(totalNumberOfGrades(person1); = 6