Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

Hello, I am really struggling to understand how to tackle this. I am wondering where in the previous lesson we learnt how to do this? It feels like this has not been taught so far and we are suddenly expected to create a function to find averages of two arrays, with no info on how to do this? I initially tried by defining the arrays of scores as score and score2 at the top. But having read other posts on the forum this seems incorrect. I also have no idea why there are two console logs at the bottom with all the scores.

Your code so far


// User Editable Region


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

let score2 = [45, 87, 98, 100, 86, 94, 67, 88, 94, 95];

let total = "[92 + 88 + 12 + 77 + 57 + 100 + 67 + 38 + 97 + 89 + 45 + 87 + 98 + 100 + 86 + 94 + 67 + 88 + 94 + 95]";*/


function getAverage(score) {
let sum = 0;
for (let i = 0; i <= score.length; i++){
    sum+= score[i];
    }
    return sum / score.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/131.0.0.0 Safari/537.36 Edg/131.0.0.0

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 1

You were taught all the pieces you need for this. This Step is about you coming up with a plan for using that syntax you’ve learned to do what the instructions are asking for.

this is not so bad. You should add some console.logs to check the values of everything to do some debugging

1 Like

the two console.logs you may notice are calling getAverage and passing an array to it. It is two examples of how the function can be used, and can be used to see if your code give the correct output

1 Like

Thank you. How does the function know to assess the array as the ‘scores’ when this has not already been declared as a variable?

What do you think the score here does?

it’s how function works. When you create a function you can give it parameters, for example here it is one parameter named score

when the function is called the parameters take the value of the passed in arguments

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

1 Like

Ok, thank you for the helpful explanation, I think it makes a little more sense now.

So, it is the console log which calls the function and ensures the function uses the array outlined in the console log, each time the console log is expressed?

not really

the console.log just print to the console whatever it has as argument

in this case the argument is a function call, so once the function call finish executing and it returns a value, that returned value is printed to the console by console.log

Ok, thank you @ILM. You have been very helpful. I will go away and do further research before trying to tackle the problem again.