Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

Not sure I get this question, what exactly is it asking for to begin with?

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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 1

You see you have the scores variable. You need to find the sum of all the scores and divide them by the length of the scores to find the average. You can use a for loop and a sum variable to find the sum of the scores, for (const score of scores) like this. And for the length you can use scores.length simply.

First of all, you need to know how to calculate average.

The “scores” is an array. And it is possible to find how many elements are available in that array with the " length" property of the array. (ex: scores. length)
You can loop through each element of the array with “for” loop. (ex: for (i = 0; i < scores. length; i++))
After that it should be straight forward calculating the average.

@TheDemonGuard so the loop calculation would be:

for (let i = 1; i <= count; i++) {
}

?

an array starts from 0, and what is count?

Do you know how to traverse array?
Use the “i” as the index to go through each element of the array.
And find the average.

I was saying would that be the format…

When you’re iterating an array with a for loop, you generally need to start at 0 (as arrays use zero-based indexing). Also, if you’re iterating the whole array, you need to iterate up to (and including) the final index in the array. Because of zero-based indexing, this will be equivalent to the length of the array, minus one.

This code, for instance, will log all elements in the array to the console:

let array = [1, 2, 3, 4, 5]

for (let i=0; i<array.length; i++) {
  console.log(array[i]);
}

A loop can work, sure

So how would you go about the equation, the tutor prompt doesnt mention it

Whats wrong with this solution?:

function getAverage(scores) {
  for(let i = 0; i < count, i++) {
    getAverage(92, 88, 12, 77, 57, 100, 67, 38, 97, 89);
    getAverage(45, 87, 98, 100, 86, 94, 67, 88, 94, 95);
  }

}

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 are not calculating the average, you are calling getAverage inside getAverage. Also count is undefined

you need to write it yourself, the instructions tell you what to do:

to calculate the average you sum all the numbers then divide by how many numbers there are

Where do we get the scores

they are passed to the function when the function is called, for example here:

for this function call, scores is [92, 88, 12, 77, 57, 100, 67, 38, 97, 89]

Whats wrong with this code?:

function getAverage(scores) {
  for(let i = 0; i < 100, i++) {
    sum(92, 88, 12, 77, 57, 100, 67, 38, 97, 89);
    length(45, 87, 98, 100, 86, 94, 67, 88, 94, 95);
  }

}

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

Can you explain in what way your code should make the sum of the values in the scores array? You are not mentioning it once

was the sum treated in a previous lesson

How to sum two numbers? Yes.
Do you remember you created a function called addTwoNumbers?

Can you remember what lesson this was