Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

Step 1
A teacher has finished grading their students’ tests and needs your help to calculate the average score for the class.

Complete the getAverage function which takes in an array of test scores and returns the average score.

The average is calculated by adding up all the scores and dividing by the total number of scores.

average = sum of all scores / total number of scores
A couple of function calls have been provided for you so you can test out your code.

Tips

You can use a loop to ite

Your code so far


// User Editable Region

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

}
return 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 (Macintosh; Intel Mac OS X 10_15_7) 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 @awet !

So the goal of this step is to get the average for a list of scores.
So the step gave you this equation

average = sum of all scores / total number of scores

so you need to figure out this part first

sum of all scores

How do you get the sum of all numbers in a array?

It looks like you were starting to do that here but have some syntax errors.

so whenever you are stuck, and not sure how to do something in programming, you can google it.

in this case, you can google “how to get sum of all numbers in array javascript”
when you do that, you will get dozens of results on how to do that like this result here

once you get that working, then you need to return this calculation here

average = sum of all scores / total number of scores

so you don’t want to return the length like you are trying to do here

instead you need to return this calculation because that calculates the average of the scores

sum of all scores / total number of scores

hope that helps

5 Likes

Thanks Jwilkins for your clear explanation. It really helps me :+1:t2:

2 Likes

Hi @awet !
Please use a while loop instead of a for loop to iterate through the scores. I was facing the same issue with the for loop, but after switching to a while loop, everything worked fine. I have attached a screenshot of my code for your reference.

Happy coding!
code removed by moderator

1 Like

hey @mrigank.kb

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

2 Likes

Thank you @im59138 for the information. I will make sure to take care of this next time :+1: :bowing_man:.

1 Like

Thank you, for the approach you have left here and for showing the way how to face such challenges.