Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

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

hi there,

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?

Hi there,

It supposed to sum up all the values from test scores, divide by numbers of students, and return the average score.

What I’m having right now is caculate the sum and define length for getAverage, while asking the function to return the values for average scores.

let’s stop here for a bit.

First thing you said is: sum up all the values from the test scores.
So, where does getAverage get these scores?

I’m supposed to define them as variable, like so:

let getAverage = [92, 88, 12, 77, 57, 100, 67, 38, 97, 89];

that’s actually not true.

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?

Yes I understand. I removed the define variable for getAverage. Should I remove the definition for length as well?

Here’s what my code currently looks like. It currently returns the full score board on the console.

function getAverage(scores) {
  let length = getAverage.length;
  let sum = 0;
  let average = scores.sum/scores.length;


  for (let i = 0; i < scores.length; i++) {
  sum += getAverage[i];
  return scores;
}
console.log(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]));

You should be able to answer that if you think: what is getAverage? Is it something I need to find the sum of all the scores?

1 Like

My apologies but I don’t really understand your hints. I assume that means:

  • I should remove the definition for length
  • getAverage is the function that if worked, should return me the average scores
  • I need to find the sum of all the scores if I want to caculate getAverage
    (Bear with me please I could be wrong)

Remove the return statement from the for loop block

No problem.

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?)

1 Like

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?

Yes creating a loop sounds like a plan.

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?

Why do you assume you are wrong?

Btw I shielded your loop code temporarily as it displays part of the solution. I will eventually remove it to avoid spoiling the solution code.

Oh thanks for doing so.

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.

Yay!

Now I just need to follow up with you on your earlier questions.

Do you still have some that were not answered?

No I pretty much understood most of the hints you said, should be fine for now.
Thanks for asking though!

1 Like