Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

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

Hi there, have you had a chance to look at other posts on the forum to see if they help you?

For eg. This one?

  1. Your for loop should use curly brackets to show what data is inside.

basic for loop example:

for (let i = 0; i < numbers.length; i++) {

}
  1. You should have also put your let some = 0 declaration within the scope of your function. Here is an example of how local scope works.

a. myVariable is outside the scope of the function, so it would be global scope.

let myVariable = 0;

function getNumber () {
}

b. myVariable is inside the scope of the function, so it would be local scope. (This is what we want for the function)

function getNumber() {
  let myVariable = 0;
}

Your code should return the correct average after you make those changes.

Hope this helped :slight_smile:

1 Like

Please do not write solution code. Thanks

1 Like

:sweat_smile: sorry, I’ll try to make examples instead, my bad.

1 Like

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!

1 Like

awesome! Glad I it helped. :smile: