Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

Hello lovely Camper Helpers out there!
I tried all methods available, reset the lesson too, yet it still says “2. getAverage[…] should return 71.7”. What am I missing?
Here is what I did:
function getAverage(scores) {
let sum;
let average;
for (let i=scores[0]; i<=scores.length; i++)
return sum+=scores[i];
return average=sum/scores.length;
}

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;
  let average;
  for (let i=scores[0]; i<=scores.length; i++)
    return sum+=scores[i];
    return average=sum/scores.length;
}


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

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 1

what do you think happen when the first return inside the loop is met?

We would have a big number = the total of the induvidual students’ scores added up. I though at this point we should break out(?) and use that number in a different funtion, but haven’t figured out how so far. I tried with a new set of {}.

the for statement is a loop. That is, it repeats everything you write within its body.
For eg this is a for loop that logs a number each time it loops:

for (let i = 0; i < 10; i++) {
  console.log(i); //this will log 0 the first time, 1, the second, etc.
}

Now look at your for loop. Does your for loop have a body? (the body is the code within the parenthesis)

I thought I filled up the parenthesis of the for loop correct, but if you ask it like that I guess not :smiley: Another thing: Do I get it right that instead of a return I should use a console.log to print the result?

no, the console.log was an example

the issue if you put the return inside the loop is that the return stops the loop and the function, so you should not

Sorry I still don’t get it. This is where I am and feel like and idiot. It says “1. Your getAverage function should return a number.”

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

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

you need to give it a starting value if you sum to it, undefined + 4 does not give good results

you already declared sum, don’t use let here

do not put an assignment in the return statement, you can return directly. Considering this you don’t even need to create average variable


why scores[0]? you have no idea what number it is, and usually it’s bigger than scores.length so i <= scores.length starts as false and your loop doesn’t go. Find a better value than scores[0] to start your loop at

super, thanks! That helped!