Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

Trying to figure out why my code isnt working. Please help.

Your code so far


// User Editable Region

let sum = 0;

function getAverage(scores) {

while (let i = 0; i < getAverage.length; i++) {
  sum += getAverage[i];
}

return sum / scores.length;
}

console.log(sum);
'
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 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0 whid/fwa6 macaddress/843a5b1846b3

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Hello there.
This is not the correct way to loop through items. Use a for loop.
In the loop, you are getting the numbers of items in the scores parameter, why do you have the getAverage function?

To access the elements of the array, you should use scores instead of the getAverage function here

Also, declare the sum variable inside the function, that way, it’ll be limited to that scope .Declaring it outside will give it a global scope This also means you should delete the console log cause it will lead to a reference error since the sum is not declared within its scope.

Thank you! Solved it

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.