Tell us what’s happening:
For some reason, I keep getting an error message that says the avg for the first array should return 71.7, even though that’s exactly what is being returned in my output. Further, the avg for the 2nd array is returning a number above 100, so I know something wonky is going on somewhere in the function. I suspect it is adding the sums of both arrays together (instead of only the sum of the values within the 2nd array), but I can’t figure out why.
Your code so far
// User Editable Region
let sum = 0;
let avg = 0;
function getAverage(scores) {
for (let i = 0; i < scores.length; i++) {
sum += scores[i];
}
let avg = sum / scores.length;
return avg;
}
console.log(getAverage([92, 88, 12, 77, 57, 100, 67, 38, 97, 89]));
console.log(getAverage([45, 87, 98, 100, 86, 94, 67, 88, 94, 95]));
// User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6.1 Safari/605.1.15
Challenge Information:
Review JavaScript Fundamentals by Building a Gradebook App - Step 1
Move the sum
variable declaration inside the function. Also, you do not need the avg
variable declaration that is outside the function.
The sum variable is inside the function already, I tried moving it outside the for loop but still inside the function, but it introduced an error where it couldn’t find the I variable. Also, deleting the avg declaration changed the output to 0s instead of the results from the avg functions.
The sum
variable declaration needs to be inside the function.
You already have avg
declared inside the function, so that is what is used and returned by the function. That is how scope works.
In case, what I said to do didn’t make sense.
function getAverage(scores) {
let sum = 0;
for (let i = 0; i < scores.length; i++) {
sum += scores[i];
}
let avg = sum / scores.length;
return avg;
}
Ahh ok I see, it is calculating the avgs correctly now. Just for clarification, why is it that the sum variable does require pre-emptive declaration and the avg variable doesn’t? From what I understand, they are both sub-functions within the overall getAverage function. I could see why either both or neither would need it, but one and not the other, that is throwing my head for a loop.
sum
is getting assigned a new value using +=
on each loop iteration. So two things needs to happen:
- It has to have an initial value you can use
+=
with, otherwise you would use +=
with undefined
.
- It should be declared one time before the loop. If you redeclare it inside the loop it would be a new variable on each iteration and would never sum up the total.
avg
is only used to get a single value, the average. So you can have the declaration and initialization in one place.
Both variables are only used inside the function and the result of using them is returned out. Whenever that is the case, they should be local function variables.
You should always use as local a scope as possible for variables. It avoids general issues with top-level/global variables, including issues they can cause with the tests.
1 Like