Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

I’m not sure where to start, I understand thee mathematics behind it but not how to write this in JS.

Your code so far


// User Editable Region

function getAverage(scores) {

}

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 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 1

1 Like

The pyramid lesson was a bit confusing, and I wasn’t able to commit much to memory.

Based on the instructions, what do you think you need to do first?

1 Like

It says to use a for loop, but I’m not quite sure what that’s for/how to use it (as I said the pyramid lesson was rather confusing for me)

That’s okay. Small steps. What will you use the loop for?

1 Like

to add up the scores

Yes. So, the first thing you need to do is create a variable to hold the value of the sum.

1 Like

Like this?

function getAverage(scores) {
let i = 0;
for () {

}
}

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 can use i for the variable name, but it’s always best practice to name your variables so you know what they contain. So, sum might be a better variable name, right? That is just a suggestion.

Now, look at the parameter that is passed to getAverage(). What data type is scores?

1 Like

Do you mean the function?

Yes. The function you are about to define is getAverage() and it takes a parameter scores.

Take a look at the console.log() statements where that function is being called. What is the data type of what is being passed into that function?

1 Like

The data that’s being called are arrays.

Yes. An array is being passed into the getAverage() function as a parameter called scores.

In order to get the sum of all the numbers in the array, you will want to loop over that array and keep adding the values to your sum variable. That’s where the for loop comes in handy.

The for loop has three parts separated by semicolons. In the first part you would declare a variable to use as a counter. Typically, i is used for that, but you can name your counter whatever you want. Since it will change, use let to declare your counter (or iterator).

Here is an example of a for loop:

for (let i = 0; i < cars.length; i++) {
  text += cars[i] + "<br>";
}
1 Like

like this?

function getAverage(scores) {
  let sum = 0;
  
  for (let i = 0;  ) {
  }

}

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]));

Yes, that’s good. Now, the second part of the for loop is the condition that will cause the loop to stop running. Since you want the loop to stop running when there are no more numbers in the array, you would want your counter (i) to be less than the length of the array, right? So, take a look at the example I added in the last post and go ahead and write the condition.

1 Like

Like this?

function getAverage(scores) {
  let sum = 0;
  
  for (let i = 0; i < 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]));

Yes, that’s perfect. The last part of the for loop is just incrementing the counter you created, and you can refer to the example again for that.

Then, the fun part. You will want to add each value in the array to the value of sum. Looking at the example again, I’ll bet you can write that now, too. Remember, you can access each value in an array by using the index of its location in brackets.

You can think of the counter (i) as the index of each value in the scores array.

1 Like

Is this what you mean?

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

The incrementor in your for loop looks good, but it looks like you’re confused about how to access elements in the scores array. getAverage() is the function you are defining; scores is the array. Please try again.

1 Like

I see what your saying, so like this?

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