میانگین یک ارایه in javascript

To find the average of all the values in a JavaScript array, you can use the following function:

index.tsx
function average(arr) {
  const sum = arr.reduce((acc, curr) => acc + curr, 0);
  return sum / arr.length;
}
109 chars
5 lines

This function takes an array arr as input, uses the reduce method to calculate the sum of all the values in the array, and then divides that sum by the length of the array to obtain the average.

Example usage:

index.tsx
const values = [2, 4, 6, 8, 10];
const avg = average(values);
console.log(avg); // Output: 6
93 chars
4 lines

gistlibby LogSnag