Calculate the average of the numbers in an array of numbers. The array: [2, 5, 17, 81, 9].
Solution
Explanation
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!
Follow me on Instagram and don’t miss the latest Challenge!
Calculate the sum of odd numbers greater than 9 and less than 30 a for-loop .
Solution
Explanation
To accomplish our task, we should remember challenge #02 because back then, the task was to print out every odd number below 99. That means we create a for loop with an increment of 2 after each iteration to get only odd numbers when starting with an odd number. In our case with 11.
Remembering challenge #05 makes us also happy again. There we had the task to create the sum of the numbers from 1 to 24. The same concept applies to this task. We add up every odd number above 9 and below 30 to a sum.
After the loop has finished, we print it out to the console:
Accumulated value printed out to the console (Sum of all odd numbers >9 & <30)
Follow me on Instagram and don’t miss the latest Challenge!