Calculating Sum and Average of Array Elements
To compute the sum and average of an array's elements, follow these steps:
Summation:
Example:
const elmt = [0, 1, 2, 3, 4, 7, 8, 9, 10, 11]; let sum = 0; for (i = 0; i < elmt.length; i++) { sum += elmt[i]; } console.log(`The sum is: ${sum}`);
Averaging:
Example:
const avg = sum / elmt.length; console.log(`The average is: ${avg}`);
The above is the detailed content of How do I calculate the sum and average of elements in an array?. For more information, please follow other related articles on the PHP Chinese website!