Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

Hey, my loop doesnt work i guess. Ive watched the other solution and fixed my problems, but my loop just iterate the first number in this array and than it stops.

Your code so far


// User Editable Region

function getAverage(scores) {
  for(let i= 0; i < scores.length;i++){
    let sum = 0;
      sum = sum + scores[i];
    return 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/132.0.0.0 Safari/537.36

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 1

do you know what happens when the return is executed?

you need to consider what has to happen only once and what multiple times

It gives back whats defined in the return? But he can only give 1 result back?
But everything is happaning just 1 time? But it doesnt fix my problem.
The function tells what to do with each number in the array right? So console log sends the 92 to the function, function is doing what it should do, and than console.log gives next, 88, to the function?

Ive changed the code to:

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

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

and how many times do you need to declare the sum variable or return it?
you need to consider what needs to go in the loop and what doesn’t

1 Like

thx iv got it <3
it cant work if the return is in the loop.

1 Like

there is an other thing that you needed to move from the loop, did you do that too?