Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

I’m struggling to pass this code during the first step. I have created a for loop within the function and added a return statement. However, I keep receiving the message that “length” is undefined.

function getAverage(scores) {
let sum = 0;
for (let i = 0; i <= scores.length; i++){
sum = sum + scores(i);
};
return sum / scores.length;
}
getAverage();
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 code so far


// User Editable Region

function getAverage(scores) {
let sum = 0;
for (let i = 0; i <= scores.length; i++){
  sum = sum + scores(i);
};
  return sum / scores.length;
}
getAverage();
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 1

as scores is an array, you should use a square bracket notation to access the contents of the array. So instead of writing it this way:

you should replace the parenthesis with square brackets.

in addition, in this line of code, you are trying to call getAverage with no parameters at all so this will not work.

as a tip for future posts: please indent your code neatly before sharing it. It allows people to read your code faster and may also help you find bugs in the future.

Edit: the article below is a good read about indentation practice even though it is not about javascript:
https://codehs.gitbooks.io/introcs/content/Programming-with-Karel/how-to-indent-your-code.html

I think your error is in this line:

Can you spot it?

Thanks. Yeah. I’ve sussed the issue.