So, we can calculate the average of our average = 0 in the second then callback function:
// Andrew has a 83% in the class
const getStatus = (userId) => {
var user;
return getUser(userId).then((tempUser) => {
user = tempUser;
return getGrades(user.schoolId);
}).then((grades) => {
var average = 0;
// average
// return our string
});
};
Now, we've been using const throughout the course. We can actually switch our var over to let; let is the ES6 equivalent to var, so this is a variable whose value we can change:
// Andrew has a 83% in the class
const getStatus = (userId) => {
let user;
return getUser(userId).then((tempUser) => {
user = tempUser;
return getGrades(user.schoolId);
}).then((grades) => {
let average = 0;
// average
// return our string
});
};
Now we're going to start off with an average of 0 and move on to actually calculating a better average if there are grades: grades.length. If grades.length is greater than 0, we're going to go ahead and actually run a calculation.
}).then((grades) => {
let average = 0;
if (grades.length > 0) {
}
// average
// return our string
});
Now, we're going to use a few array methods here. First up, we're going to set average equal to some value. We're going to kick things off by taking our array of objects and getting it down to an array of numbers.
We'll do that using map; that's grades.map. Here, we're going to go ahead and get access to the individual grade and all we're going to do is implicitly return grade.grade.
if (grades.length > 0) {
average = grades_map((grade) => grade.grade)
}
So, we have the individual grade object and we're trying to access its grade property. At this point, we have an array of numbers. We need to turn those numbers into a sum and then we have to divide that by the length of the array. We'll be using reduce here, so we call reduce on an array of numbers. reduce works a little differently than some of the other array methods you might have seen in the past. This one takes two arguments, a and b;
if (grades.length > 0) {
average = grades.map((grade) => grade.grade).reduce((a, b) => {
});
};
So, the first time it goes through, it's going to take the first two grades and we'll be able to do something with those grades. What do we want to do? We want to return a + b. Then it's going to take that sum for the first two grades, it is going to call the reduce function again, putting that sum and putting the third grade. We'll take a + b to get that value added on to the new b and then we'll continue to generate that sum. Now, you can actually simplify that a + b:
if (grades.length > 0) {
average = grades.map((grade) => grade.grade).reduce((a, b) => a + b);
}
Now, this alone just gives us the sum so, in the case of Andrew, we haven't calculated the average 83; we've just added up the two numbers. You also want to divide this by grades.length; that is what's going to give us the average. We can go ahead and test this out by printing the average variable, console.log (average).
if (grades.length > 0) {
average = grades.map((grade) => grade.grade).reduce((a, b) => a + b) / grades.length;
}
console.log(average);
});
I'm going to save it. We have getStatus and we have getStatus for 1. That is perfectly fine, we can continue to use that. In the terminal, we get 83 printing, which is the correct average.

If I go ahead and rerun it for user 2, we get 100 printing. Everything is working really well; undefined is just coming up because we don't have anything returned so status equals undefined, which prints in the console.log statement. So, instead of just dumping average to the screen, let's go ahead and actually return our template string. This is the final thing we're going to do in this section.