Welcome to Stackoverflow :) We will be happy to help you by learning our tools better. Only one note about the algorithm: move the middle calculation command outside the loop:
for(var x = 0; x < scores.length; x ++) { Sum = Sum + scores[x]; //or Sum += scores[x]; } average = Sum / scores.length; //length of the array scores is in scores.length
I would use parseInt() instead of new Number() , because new Number() creates an object, and parseInt() gives you the actual value of the letter as the result. (higher performance).
By the way, do not forget to put var in front of each variable definition if you do not want them to be accessed globally (bad idea). You did a good job with all variables except scores . The definition should be var scores , although this is not the source of this error.
Another point: you can check if the result of parseInt() works with the isNaN() function. If your numbers can have decimal points, you can also use parseFloat() :
The result of both functions is NaN (not a number) if conversion from string to number is not performed.
And finally, I think it is a good idea that you define an array with the specified length. This improves the readability of your code. However, this is not necessary in Javascript, as it automatically increases / decreases the length of the array at runtime, so you do not need to determine in advance how long it should be. It can be good or bad, depending on how you use it. But in general, you can use var myarr=[]; instead of var myarr= new Array(); . However, when you want to hint to other developers what is happening, you can also specify the length of the array: var myarr=new Array(4); .
And the last thing to use Stackoverflow: accept the best answer and try other useful answers. In this way, you will get a rating from other people.
Good luck.
source share