Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

My getAverage function returns the right number but doesn’t pass codecheck:

let sum = 0;
let numbers = 0;

function getAverage(numbers) {
for (let i = 0; i < numbers.length; i++) {
sum = sum + numbers[i];
}
sum = sum /numbers.length;
return sum;
};
getAverage([92, 88, 12, 77, 57, 100, 67, 38, 97, 89]);
console.log(sum);
sum = 0;
getAverage([45, 87, 98, 100, 86, 94, 67, 88, 94, 95]);
console.log(sum);
sum = 0;
getAverage([38, 99, 87, 100, 100, 100, 100, 100, 100, 100]);

Your code so far


// User Editable Region

let sum = 0;
let numbers = 0;

function getAverage(numbers) {
    for (let i = 0; i < numbers.length; i++) {
      sum = sum + numbers[i];
    }
    sum = sum /numbers.length;
    return sum;
  };
getAverage([92, 88, 12, 77, 57, 100, 67, 38, 97, 89]);
console.log(sum);
sum = 0;
getAverage([45, 87, 98, 100, 86, 94, 67, 88, 94, 95]);
console.log(sum);
sum = 0;
getAverage([38, 99, 87, 100, 100, 100, 100, 100, 100, 100]);
console.log(sum);



// 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/124.0.0.0 Safari/537.36

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Hi there and welcome to our community!

Your issue is your scoping of the variable sum. Neither sum nor numbers should be global variables.

The variable numbers doesn’t need to be declared at all, as it is the function parameter and will be assigned a value when the function is called.

The variable sum should be declared at the top of your function body. It doesn’t need to be accessible outside of the function, except as a return value when the function is called.

If you wish to see the result of calling your function, remove all instances of console.log(sum) and sum=0 from the following code:

You can then simply wrap a console.log() around the remaining function calls.

Your function itself works just fine.

avoid the assignment on the return line, it’s not good code

(post deleted by author)

hey there!
it is great that you have passed your challenge step. but you should not post your working code solution here on the forum. that is do not allowed on the fcc forum.
@riconthelake

How do I remove the code i posted?

you can delete any of your post by pressing delete option. within reply option of your post, you seen three dots option?. when you press that option you will find delete option.
@riconthelake