Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

My code so far is:

scores = [92, 88, 12, 77, 57, 100, 67, 38, 97, 89];
for (let i = 0; i < scores.length; i++) {
scores += scores[];
return scores/scores.length;
}
scores = [45, 87, 98, 100, 86, 94, 67, 88, 94, 95];
for (let i = 0; i < scores.length; i++) {
  scores += scores[];
return scores/scores.length;

A comment after testing was:
SyntaxError: unknown: Unexpected token (4:17)
May I know where I was mistaken?

Your code so far


// User Editable Region

function getAverage(scores) {
scores = [92, 88, 12, 77, 57, 100, 67, 38, 97, 89];
for (let i = 0; i < scores.length; i++) {
scores += scores[];
return scores/scores.length;
}
scores = [45, 87, 98, 100, 86, 94, 67, 88, 94, 95];
for (let i = 0; i < scores.length; i++) {
  scores += scores[];
return scores/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]));

// 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/125.0.0.0 Safari/537.36 Edg/125.0.0.0

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 1

HI @haahinnawy !

It looks like you are wrote a similar thing twice here

So I would suggest resetting the code.

Here is the problem you are trying to solve.
You need to calculate the average for a list of numbers

The lesson gives you this formulae here

average = sum of all scores / total number of scores

There are two parts to this problem

  1. How do you get the sum of all scores?
  2. How do you get the average?

You need to figure out the first part

It looks like you started to do that but had some issues with the logic.

Part of learning to program is learning how to problem solve.
If you don’t know how to do a particular part of the problem then google it.

For example, you can google “how to add up numbers in an array javascript”
There will be tons of articles to help you and walk you through it.

Once you figure that out, then you can figure out how to how do you get the average

hope that helps

Thank you. My code now is:

scores = [92, 88, 12, 77, 57, 100, 67, 38, 97, 89];
let sum = 0;
for (let i = 0; i < scores.length; i++) {
sum += scores[i];
return sum/scores.length;
} 

I didn’t ask for a test as the returned value is 9.2 where the average must be 71.7.
I reset and worked it out again to get the same result.
Something is wrong in my code?

Hello!
always post your code using the preformatted text </> option. or manually add three back ticks (```) in a seperate line, above and below your code.

move your retrun value out of for loop. place it after closing } for…loop bracket.

and remove the above assignment.
@haahinnawy

Thanks a lot. Now I got the correct result although I didn’t ask for a test yet. I tried to complete with the ssecond scores array and my code was:

scores = [45, 87, 98, 100, 86, 94, 67, 88, 94, 95]
let total = 0;
for (let i = 0; i < scores.length; i++) {
total += scores[i];
}
return total/scores.length;
,
I didn’t get a result. Just the result for the first scores array was reprinted. I changed sum using total in order to avoid this but I failed. So, may I know what is wrong? Am I asking too much?

First of all. The way you posting your code here is incorrect. I said you in my previous post that, post your code correctly. remember don’t post a piece of code, post your whole function that you working on it.
@haahinnawy

Sorry, Mr. Hassan. This is the full code.

function getAverage(scores) {

scores = [45, 87, 98, 100, 86, 94, 67, 88, 94, 95]

let total = 0;

for (let i = 0; i < scores.length; i++) {

total += scores[i];

}

return total/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]));

The challenge provides two arrays of scores. When I did the first of them the correct value was returned but the code didn’t pass and the comment mentioned the second array and what its result should be. When I did both the code for the second one appeared pale making me think that there was something wrong. In the meantime the variable sum was commented on as it was executed before. Thus I changed to use the variable total with the second array. Any way the code didn’t pass either, and even the correct value didb’t appear. At last I did them separately, and the written code above is for the second array, which returned teh correct value but again the code didn’t pass. I am confused.

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

The challenge provides two arrays for you to test your code.
But that doesn’t mean you should write your code to try to pass for only those two arrays.
That is why this is incorrect

You should not be trying to update the scores array to fit that one test case.

Your function should work for hundreds of test cases.
Not just the two that you can see.

That is why you are creating a function so it can be reusable and it should work for any array of numbers that is passed into it.

hope that is clearer where your mistake is

Sorry, this is more confusing. If I don’t have to update the scores to test my code why the test reminded me of the scores that I didn’t use. In action, I was not in need of a test to verify the code as I have already got the correct average value for both scores arrays when I dealt with each separately. Only when I dealt with both simultaneously and after the test when I dealt with each separately that I knew the code doesn’t pass. Why do I get a correct value with a code that was considered as wrong. I understand from the beginning that a function is reusable, and I reused it by dealing with the two arrays subsequently and separately. Should I write a code without using any specific array, or should I use different arrays written by me? I don’t understand you.

unfortunatley, that is not what resuable means though.

I think the issue is that you are not understanding parameters and arugments.
I would suggest reading through this article

You don’t need this line at all

scores is a parameter.
A parameter acts as a placeholder for the real value when the function is called.

when getAverage is called here

scores is now this array

when getAverage is called here,

scores is now this array

If I called the function again like this,

console.log(getAverage([94, 68, 33, 86, 95, 100, 57, 76]));

then scores is now this array

[94, 68, 33, 86, 95, 100, 57, 76]

Imagine this scenario, what if the lesson wrote 50 function calls like this?

console.log(getAverage([5, 10, 15, 20]));
console.log(getAverage([33, 44, 55, 66, 77, 88, 99]));
console.log(getAverage([15, 25, 35, 45, 55, 65]));
console.log(getAverage([7, 14, 21, 28, 35, 42, 49]));
console.log(getAverage([18, 36, 54, 72, 90]));
console.log(getAverage([100, 200, 300, 400]));
console.log(getAverage([75, 85, 95, 105]));
console.log(getAverage([12, 24, 36, 48, 60]));
console.log(getAverage([11, 22, 33, 44, 55, 66, 77]));
...

Would you then try to set scores 50 times like you are trying to do now like this?

function getAverage(scores) {
    scores = [5, 10, 15, 20];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
        total += scores[i];
    }
    return total / scores.length;
}

Can you see why trying to set scores like this scores = [5, 10, 15, 20]; wouldn’t be practical is you had 50 test cases to work with?
It would take a lot of time having to manually undate this array each time

that is why your approach isn’t working but also not needed.

I think reading through that article on how parameters work will help you understand the underlying concept better.

hope that helps

Thank you very much. You have explained what was said before in the curriculum about calling a function, but there was not enough applications to make it so clear. However, I still have a problem applying this concept. Here is what I did:

   function getAverage(scores) {
let total = 0;
for (let i = 0; i < scores.length; i++) {
getAverage([92, 88, 12, 77, 57, 100, 67, 38, 97, 89]);
total += scores[i];
return total/scores.length;
}
}

I think I here call the function, but once I did I got this comment:
RangeError: Maximum call stack size exceeded
Then I didn’t get the average value like what I got earlier, and after the test I got a comment that the function should rerurn a number, which is something I know. So, what is wrong here in calling the function and why the code doesn’t return a result? I know you shouldn’t give a direct answer and should push me to think but I don’t understand the comments given in the challenge. What is the call stalk size that was exceeded? What is wrong?

You called the getAverage function and return statement within the for...loop . Move it after the loop body.

You shouldn’t call the function here

You don’t need to call the function at all.
The lesson already did that for you here

You are getting that error because you are continually calling the getAverage function inside itself.

Without getting into to much detail, you are attempting to create a recursive function without a base case.
You will learn more about recursion in a later project.

But for now, just know that you need to delete this line here

and as mentioned earlier, your return should be outside of the for loop.

you did it correctly here before you changed it

Well, Thank you. I did but I didn’t get a number result but I got (NsN), and the code didn’t pass.

Thank you. Across my questions so far I got more useful information. I am happy for that but the original problem is not solved yet. Now, my code is:

function getAverage(scores) {

let total = 0;

for (let i = 0; i < scores.length; i++) {

total += scores[i];

}

return total/total.length;

}

On the other hand, the result is (NaN) and the test result is the same as it was at the beginning:
// running tests
getAverage([92, 88, 12, 77, 57, 100, 67, 38, 97, 89]) should return 71.7.
getAverage([45, 87, 98, 100, 86, 94, 67, 88, 94, 95]) should return 85.4.
getAverage([38, 99, 87, 100, 100, 100, 100, 100, 100, 100]) should return 92.4.
Your getAverage function should return the average score of the test scores.
// tests completed
// console output
NaN

I look like being turning in a vicious circle.

Your issue is here

you shouldn’t have total.length
that wouldn’t work.

remember that total is a number

remember that the length property can be used on arrays.

once you fix that then it will pass

You did it correctly here, but then you started to change to many things

Refer back to what you wrote here

The good news is that you were really close to passing.
But I think that you are getting frustrated and unfortunately changing things that were once correct and making them incorrect.

So when it comes to making changes, make sure to only make one change at a time and test more often.
That way you won’t accidentally break something that was previously correct

hope that helps

3 Likes

That is right. Thank you. I was blinded with too many frustrations. More thanks to you.