The console keeps returning NaN. I’m a bit confused so any hints and advices about what needs to be fixed, and why is that in details would be greatly appreciated.
Thanks in advance!
Your code so far
// User Editable Region
function getAverage(scores) {
let getAverage = [92, 88, 12, 77, 57, 100, 67, 38, 97, 89];
let length = getAverage.length;
let sum = 0;
for (let i = 0; i < scores.length; i++) {
sum += getAverage[i];
return sum/length;
}
console.log(sum);
console.log(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/127.0.0.0 Safari/537.36
Challenge Information:
Review JavaScript Fundamentals by Building a Gradebook App - Step 1
we can definitely help you but first, let’s work through the problem first.
Can you describe first (in your own words) what getAverage(scores) function is supposed to do?
If you look at the function definition given to you, it looks like this:
function getAverage(scores) {
}
So this function called getAverage is expecting a parameter called scores.
This scores parameter will contain the scores we need to sum.
Do you know then if anything you wrote in your original code should be removed because we know that scores is the array of scores we are supposed to add?
I asked earlier: what is this getAverage? You are trying to find its length? So it is a ? Is it a function as you say? What is the length of a function? These are questions that would occur to someone who started to read your code. Obviously getAverage is a function if we click the reset button in this step. It is given to us as a function. But your code initially did this:
So initially, you had told your browser that even though you had a function called getAverage, actually now, you wanted to make an array called getAverage.
Then in our last exchange, I asked you, what does the function getAverage do? You said it is a function to get the average score .
After that I asked you where does getAverage get the scores it sums up.
We arrived at an answer which was: it gets the scores from the arrays of scores given to it which is called scores.
After that I said, well if we have a getAverage function and an array of scores, do we need all the code that you wrote before? What don’t we need?
You said we don’t need this:
But you were confused about the length line that was getting the length of getAverage.
So then I asked? Read the line. Without the previous let line, isn’t getAverage the name of your function?
So what is length supposed to do? (Do you need the line that gets the length of the get average function?)
I see. So getAverage already got its arrays of scores which is “scores”. Therefore I don’t need to define its “length” and redefine getAverage as a new array, is this correct ?
So what I should do now, is to first create a loop which caculates the sum for getAverage, isn’t it?
Great. So for now I removed the necessary code to caculate the average scores, which is:
let sum = 0;
for (let i = 0; i < scores.length; i++) {
sum += scores[i];
}
console.log(sum);
}
It works and it returns the sum for 2 scores, which are 717 and 854, so next I need to define the array “average” as sum/scores.length, which should be something like this:
let average = sum/scores.length;
Assuming these are incorrect, where should I start to look at?
Also I figured it out. So I don’t need to define a new array called “average”, but instead I should just ask js to return the result similar to the example code.
Thank you so much for bearing with me! Those were extremely helpful and detailed.