I have no idea why this doesnt work. I have tried almost everything, including this guide here https://www.freecodecamp.org/news/how-to-add-numbers-in-javascript-arrays/. I literally copied the first example code, changed some variables to have proper names and it still wouldnt give me atleast a sum of the grades. It just outputs the first number in each inputted array.
Your code so far
// User Editable Region
function getAverage(scores) {
const totalNumGrades = scores.length;
let gradeSum = 0;
for (let i = 0; i <= scores.length; i = i + 1) {
gradeSum += scores[i];
return gradeSum
}
return gradeSum / totalNumGrades;
}
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/123.0.0.0 Safari/537.36 OPR/109.0.0.0
Challenge Information:
Review JavaScript Fundamentals by Building a Gradebook App - Step 1
let’s take this step by step. The objective is not to pass the challenge, but rather, it is to learn how to code.
What is your understanding of the for loop you have here?
What were you trying to make it do and what do you think it actually does?
Things to help you think about these things:
1- look at the console as you work
2- use console.log to help you log the value of i and the value of scores[i]
you can also log anything else of interest
How i think the loop is built is the iterator starts at 0, the loop runs while i is less than scores.length (now i realized it should be <, not <=), so until its equal to 9 (loops 10 times, 0-9). The code in the loop adds an element from the scores array with the index corresponding to the iteration of the loop. Then it returns the calculated sum of all grades for use in other parts of the function. In the last return statement it calculates the avg, dividing the sum of all grades by the scores.length. Thats how i thought it works. I guess it doesnt. TIA
1)In getAverage function you return the value two times one is inside the for loop and another one is out side the loop . you should not return the value inside the for loop because it will break the loop.
2) while iterating the 9th index will enter the loop if this the last index it should break but it moves to the next iteration . so you should use condition like i<scores.length or i<=scores.length-1
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.
You’re doing great in realizing that the loop was going too far as using scores[9] would not be good idea. Awesome.
Now you need to examine this statement you made “it returns the …sum…for use in…the function”.
This statement is not true.
What does “return” do when it is used in a function?
It literally stops the function right there and exits it to where the function was called. So in your loop, the function will end as soon as it hits the return statement (watch out that you are missing a semicolon here but that is minor compared to the issue at hand).