Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

I am not sure what the problems are! I felt like I didn’t understand how a loop in a function works.

Your code so far


// User Editable Region

function getAverage(scores) {
  let sum=0;
  for (let i = 0; i < scores.length;i++){
  sum+=scores[i];
  average=sum/scores.length;
  return average
}

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/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

Loops need braces. You have the opening brace { but have neglected the closing one }

Edit: also check the console for error messages as you go. There is one more issue.

I do have the closing brace, but it is after the “return average”. Am I supposed to have the return outside of the loop?

Also the error only said the function should return a number so I am not sure what the problems are with my code? I am very new to Javascript so I am very confused!

Count how many opening braces you have in the code you shared. Then count how many closing braces you have. Do they match?

After fixing that, look at the console to find more errors.

Return statements will end the execution of the function. You can have a return statement wherever you think your function should end.

Oh yes i did miss one closing brace.
The error showed “SyntaxError: unknown: Unexpected token, expected “,” (11:65)”. Does it mean anything wrong with the console.log? But this statement is given from the beginning.

function getAverage(scores) {

let sum=0;

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

sum += scores[i];

let average=sum/scores.length;

return average;

}

}

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]));

I fixed the problem with the console. And numbers are returned but it’s still not correct.

Oh I found the problem and fixed it already! Thank you very much for your help!!!

1 Like