Task of Challenge #11/54
Calculate the average of the numbers in an array of numbers. The array: [2, 5, 17, 81, 9].
How to calculate an average
A calculated „central“ value of a set of numbers.
To calculate it: add up all the numbers, then divide by how many numbers there are.
Example: what is the average of 2, 7 and 9?
Add the numbers: 2 + 7 + 9 = 18
Divide by how many numbers (i.e. we added 3 numbers): 18 ÷ 3 = 6
So the average is 6
(Also called the Arithmetic Mean.)
Accomplishing our Goal
To accomplish our task, we create a function called
averageArray() and pass in our array.
We need to get the length of the array for our for-loop. Creating a sum and then dividing by the length gets our average.
Just return the number and print it to the console, and you are done!