I want the getAverage function to return the average for each array. However, my code now is returning the average for the first array, then the sum of the averages for the first and second arrays. How do I make it return only the average for the second array?
Thank you in advance!
Your code so far
// User Editable Region
let sum = 0;
function getAverage(scores) {
for (let i = 0; i < scores.length; i++)
sum += scores[i];
let average = sum / scores.length;
return average;
// User Editable Region
}
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]));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Challenge Information:
Review JavaScript Fundamentals by Building a Gradebook App - Step 1
You are completely right, I tried moving my “sum” variable declaration inside the scope of the function and it worked correctly. Thank you so much for the detailed answer, I really appreciate it!